Local Variables for Ruby

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
"local-variable"
ruby> loop{bar=45 print Bar, "n"; break}; defined bar
nil

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.