Ruby class instance variable magic details

Source: Internet
Author: User
Tags instance method ming

Learn a little bit about the class-related variables encountered by the syntax associated with ruby classes.
Ruby is very flexible, the syntax seems very casual, such as looking at the following code

Class Foo
Attr_accessor:name

DEF initialize
@name = ' Xiaoming '
End

DEF say
Puts @name
Puts Self.name
Puts name
End
End

Foo.new.say
Executing the code will get the following output:

Xiao ming
Xiao ming
Xiao ming
The initialize is a constructor that
Foo.new
The time automatically executes,
@name
is an instance variable, then the
Say
Method in

Puts @name
Very well understood, is the printing instance variable
@name

Puts Self.name
How do you understand it?

First, what does self represent? It and PHP's
$this
The meaning is similar to that of the example, which is used in the class method.

That
Self.name
Is that a class variable? No.

In fact, this is a method, to write out is:

def name
return @name
End
It's amazing, isn't it? So if you're wondering where this method comes from, you should have noticed that the second line of code

Attr_accessor:name
Yes
Attr_accessor:name
This is also a method, and so
Attr_accessor (: Name)
Because parentheses can be omitted in Ruby, it may be awkward to read the code at Beginner's.

: Name
is a symbol,
Attr_accessor
method depends on it to determine the
@name
Instance variable creation
Name
And
Name=
and other related methods, you can implement the external expression directly to the instance variable value, such as:

Foo = foo.new
Foo.name = ' Xiao Li ' #name和 = spaces between are ignored, Ruby ignores spaces
Puts Foo.name
Self.name
Well, let's take a look at the last one.

Puts name
What is this, here's the
Name
What should be understood.

Well, it's understandable, because calling a method in Ruby can omit parentheses, so when you see a variable, it might be a local variable, or it might be a method, a priority, and, of course, a first local variable.

We see
Say
There is no local variable defined in the method, so the
Name
Just think of it as a way of doing it.
Name ()
In fact, it's the same with the keyword self.
Self.name ()

More and more feeling ruby flexible, of course, I still think should follow certain norms, otherwise you write code other people may not understand at all.

I'm going to write the project, and within the class method I'm definitely going directly through
@name
Operation without using the
Self.name
Or
Name
To do it, don't you think?

The

Start with a variable is an instance variable, and the instance variable belongs to a particular object.
Class Person
def initialize (name, gender, age)
  @name = name
  @gender = Gender
      @age = In the example above, @name, @gender, @age are instance variables. 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. The
class variable is shared by all instance objects of a class, or it can be accessed by a class method. The class variable name begins with ' @@ ', such as ' @ @number '. and global variables, unlike instance variables, class variables must be initialized before they are used:
class person
@ @number = 0 #使用前必须有初值
def initialize (name, gender, age)
 @n AME = Name
  @gender = gender
@age = Age
@ @number + = 1
End
End
class variable is private and cannot be accessed directly outside of the class, you can only use the instance method and the class method To visit it.

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.