Ruby Variable definition detailed

Source: Internet
Author: User
Tags constant instance method lowercase valid

The first character of the Ruby variable name determines whether it is a local variable, an instance variable, a class variable, a global variable, or a constant. Typically, the second character of a variable name is a number, a letter, or an underscore, but some internal variable names are more specific
s = ' Hello world! '
x = 10
# P004STRINGUSAGE.RB
# Defining a constant
PI = 3.1416
Puts PI
# Defining a local variable
myString = ' I love the city, Pune '
Puts MyString
=begin
Conversions
. To_i, To_f,. to_s
=end
VAR1 = 5;
var2 = ' 2 '
Puts Var1 + var2.to_i
# << appending to a string
A = ' Hello '
a<< ' World.
I love this world ... '
Puts a
=begin
<< marks the start of the string literal and
is followed by a delimiter of your choice.
The string literal then starts from the next
New line and finishes when the delimiter is
Repeated again on the a line in its own.
=end
A = <<end_str
This is the string
and a second line
End_str
Puts a
A variable is named with a rule that starts with a lowercase letter or an underscore, which can contain only letters, numbers, and underscores. Keyword cannot be used as a variable name.
When the Ruby interpreter reads a word, he interprets it as a variable name, a method name, or one of the reserved keywords.
If a word is followed by a "=", it is a variable; if it is a keyword, it can only be used as a keyword; other cases are treated as method names.
In the following example:
x = "To_i".
“.” means that the method to_i is called by the string "100".
The string "100" is the caller of the method.
“.” The preceding object and the following method are calls and invoked relationships.


Take a look at the summary of the classification of ruby variables

Local variables
Cases:

Foobar
If the identifier first is a lowercase letter or "_", the identifier is either a local variable or a method call. In the scope of a local variable (class, module, definition part of a method), the first assignment of an identifier that first is a lowercase letter means declaring a local variable that belongs to that scope. If you reference an identifier that has not been declared, it is interpreted as a method call without arguments.

The scope of a local variable begins at the declaration and ends at the end of the block, method definition, class/module definition in which the declaration resides. As the block dies, local variables are also dead (the top-level local variable continues until the end of the program), but there are exceptions. If the block has become a process object, the local variable will continue until the end of the process object. If multiple process objects reference the same scope, local variables are shared by those objects.

# (A) part is outside the scope
2.times {
P defined? (v) # (A)
v = 1 # from (Start statement)
P v # to (end of block) is the scope of V
}

# => Nil
1
Nil <-, please note that this is nil.
1
This is still valid even if the declaration part is not executed.

v = 1 If False # although not assigned, but declared valid
P defined? (v) # => "Local-variable"
P V # => Nil
If you use the-k option, you can use the Japanese identifier, and the Japanese identifier is treated as a local variable. In fact, we do not recommend that you do so.

Instance variables
Cases:

@foobar
A variable starting with @ is an instance variable and belongs to a particular object. You can reference an instance variable in a method of a class or subclass. If you reference an instance variable that has not yet been initialized, its value is nil.

class variables
Cases:

Class Foo
@ @foo = 1
def Bar
Puts @ @foo
End
End
The variable starting with @@ 开始 a class variable. You define class variables in the definition of a class, and you can reference/assign values to class variables in particular methods, instance methods, and so on.

The difference between a class variable and a constant is as follows.

You can assign a value repeatedly (a constant will warn)
Cannot directly reference outside of class (can be referenced/assigned in an inheriting class)
The difference between a class variable and an instance variable of a class is as follows.

You can reference/assign values in subclasses
can be referenced/assigned in an instance method
You can think of a class variable as a global variable that is shared by classes, subclasses, and their instances.

Class Foo
@ @foo = 1
End
Class Bar < Foo
P @ @foo + + 1 # => 2
End
Class Baz < Bar
P @ @foo + + 1 # => 3
End
The class variables (module variables) defined in the module are shared by all classes that contain the module.

Module Foo
@ @foo = 1
End
Class Bar
Include Foo
P @ @foo + + 1 # => 2
End
Class Baz
Include Foo
P @ @foo + + 1 # => 3
End
Global variables
Cases:

$foobar
$/
Variables that start with $ are global variables that can be referenced anywhere in the program (and therefore require special attention). Global variables do not need variable declarations. When you reference a global variable that has not yet been initialized, its value is nil.

Pseudo variable
In addition to ordinary variables, there is also a special variable called pseudo variable.

Self
The execution body of the current method

Nil
Unique instance of the Nilclass class

True
Unique instance of the Trueclass class

False
A unique instance of the Falseclass class. Nil and false indicate "pseudo".

__file__
Current source file name

__line__
The line number in the current source file

The value of the pseudo variable cannot be changed, and if you assign a value to a pseudo variable, a syntax error will be thrown

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.