Ruby local variable _ruby topic

Source: Internet
Author: User
A local variable starts with a lowercase letter or an underscore (_). Local variables do not contain nil values like global and real variables before initialization.

Ruby> $foo
Nil
Ruby> @foo
Nil
ruby> Foo
ERR: (eval): 1:undefined local variable or method ' Foo ' for Main (Object)


The first assignment of a local variable is done much like a declaration. If you point to an uninitialized local variable, the Ruby interpreter thinks it is the name of a method; as seen above

of information.

In general, the range of local variables will be



Proc{...}

Loop{...}

Def... End

Class...end

Module...end

Entire program (unless one of the above conditions is met)



The following example, define? is an operator that checks whether an identifier has been defined. If it is defined, it returns a description of the identifier, or nil. As you can see, the bar range is

The local variable of the loop; bar is undefined when the loop exits.

ruby> foo = 44; print foo, "\ n"; Defined? Foo
44
"Local-variable"
ruby> loop{bar=45 print bar, \ n; break}; Defined? Bar
45
Nil


A scoped Process object shares this range of local variables. Here, the local variable bar is P1 by main and process objects, P2 shared:

Ruby> bar=0
0
ruby> p1 = proc{|n| Bar=n}
#<proc:0x8deb0>
ruby> P2 = Proc{bar}
#<proc:0x8dce8>
Ruby> P1.call (5)
5
Ruby> Bar
5
Ruby> P2.call
5


Note that the starting "bar=0" cannot be omitted; This assignment allows bar ranges to be shared by P1 and P2. Otherwise p1, P2 will generate and process their own local variable bar separately, calling P2

It will also cause the "no local variables or methods" error to be defined.

The strength of process objects is that they can be passed as parameters: shared local variables are still valid even if they are passed out of the original range.

ruby> def Box
| Contents = 15
| get = proc{contents}
| Set = proc{|n| contents = n}
| Return GET, set
| End
Nil
Ruby> Reader, writer = box
[#<proc:0x40170fc0>, #<proc:0x40170fac>]
Ruby> Reader.call
15
Ruby> Writer.call (2)
2
Ruby> Reader.call
2


Ruby's approach to scope is pretty smart. Obviously, the contents variable in the above example is shared by reader and writer. We can also create more pairs of box-like

Reader-writer Each pair shares a contents variable and does not interfere with each other.

Ruby> reader_1, writer_1 = Box
[#<proc:0x40172820>, #<proc:0x4017280c>]
Ruby> reader_2, writer_2 = Box
[#<proc:0x40172668>, #<proc:0x40172654>]
Ruby> Writer_1.call (99)
99
Ruby> Reader_1.call
99
Ruby> Reader_2.call
15

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.