Ruby journey (15) three variables in Ruby: a constant and two pseudo variables

Source: Internet
Author: User
Global variable $ XX
Object variable @ xx
Local variable [A-Z] xx

Constant [A-Z] xx

Two pseudo variables self and nil:
Self is actually a global variable pointing to the current object. The value of this variable is controlled by the interpreter and can only be read and cannot be modified.
Nil is also a global variable controlled by the interpreter and cannot be assigned a value.

1. Global variable $ XX
Global variables should be used with caution because their scope is script-level and can be changed anywhere in the current session.
If the global variable is accessed before the initialization, It is nil.
You can use trace_var to monitor global variable changes and bind a variable to respond to changes.CodeBlock. Trace_var: $ ABC, Proc {Print "$ ABC is now # {$ ABC. to_s} \ n"}

$ ABC= 2

The above is the event when a $ ABC change is registered using trace_var. For this global variable used as the process initiator, it is usually called the Active Variable variable in ruby.

There is also a special global variable, usually a system variable, indicating the context information of the environment.
$ Process ID of the Ruby interpreter
$! Last error message
$ @ Error location
$ _ Gets string recently read
$. Number of lines recently read by the interpreter
$ ~ The most recent matching of a child expression group
$ N the nth subexpression most recently matched
$ = Case-insensitive flag
$/Input record Separator
$ \ Output record Separator
$0 Ruby script file name
$ * Command line parameters
$? The last sub-process exited

2. object variable @ xx
The scope of the object variable is inside the object. If you access the object before the initialization, you will get an Nil.
You can directly use Ruby object variables without declaration before they are used. In fact, the object variables are dynamically added to the object during the first use. You can see the following sample. Class Sample
Def Seta
@ =   1
End
Def SETB
@ B =   2
End
Def Show
Puts " Print instance variables: "
Puts self. instance_variables
Puts " Print end "
End
End
S = Sample. New
S. Show # None
S. Seta
S. Show # Only have @
S. SETB
S. Show # Only @ A and @ B

3. Local variable [A-Z _] xx
It starts with a lower-case letter or underline and does not contain the nil value before the first value assignment. The first value assignment is like a type declaration, I think the Ruby interpreter determines the type of the variable based on the first declaration.

The local variable scope is the context

4 Ruby constants [A-Z] xx

A constant can only be assigned once, but the current Ruby version only generates a warning when assigned multiple times.
Constants can be defined in classes and modules and can be accessed through the: symbol.

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.