Ruby local variables

Source: Internet
Author: User

Local variables start with lowercase letters or underscores (_). Local variables do not include nil values before initialization like global variables and real variables.

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 like a declaration. If you point to an uninitialized local variable, the Ruby interpreter considers it a method name, as shown in the preceding error.

Information.

Generally, the range of local variables is



Proc {...}

Loop {...}

Def... end

Class... end

Module... end

The entire program (unless the above conditions are met)



In the following example, define? Is an operator that checks whether an identifier has been defined. If it has been defined, the description of the identifier will be returned, otherwise nil will be returned. As you can see, the bar range is

The local variable of the loop. When the loop exits, the bar is not defined.

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


Process objects in a range share local variables in this range. Here, the local variable bar is shared by main and process objects p1 and p2:

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 "bar = 0" at the beginning cannot be omitted. This value allows the bar range to be shared by p1 and p2. otherwise, p1 and p2 will generate and process their own local variable bar separately, and call p2

This will also cause the "undefined local variable or method" error.

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 is very clever in terms of scope. Obviously, the contents variable in the above example is shared by reader and writer. We can also create many pairs of box

Reader-writer; each pair shares a contents variable, irrelevant to 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.