Ruby Variable _ruby Topic

Source: Internet
Author: User
Tags constant
Ruby has three classes of variables, one constant, and two strict pseudo variables (pseudo-variables). There are no types for variables and constants. Although there are some drawbacks to the untyped variable, there are more advantages and good compliance with Ruby's quick and easy (quick) Philosophical spirit.

In most languages, a variable must specify its type, the change (not a constant), and the scope; Because the type does not exist, the rest can be determined quickly by the variable name (which you'll see right away) and we don't need a variable declaration in Ruby.

Classify it by a first-letter identifier:

$ global variable
@ Real Variable
[A-z] local variables
[A-Z] constant

The only exception is Ruby's pseudo variable: self, which always points to the currently executing object or the null value of the uninitialized variable (meaningless value) nil. Although both names are like local variables, self is a global variable controlled by the interpreter, and nil is actually a constant. Since there are only two kinds of accidents, they will not disturb us too much.

You can assign values to self or nil. In the following example, main, as the value of self, points to the object at the highest level:

Ruby> Self
Main
Ruby> Nil
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.