我是因?yàn)橛昧薱offee所以萌生出想學(xué)學(xué)ruby的想法的,因?yàn)椴艅傞_始學(xué)習(xí),所以有些問題可能比較初級。
我看的代碼里大量使用了:xxxx
之類的語法,我從網(wǎng)上查到這個叫Symbol
,從用法上來看它的本質(zhì)應(yīng)該是不是一種不需要預(yù)定義,但是會被預(yù)編譯的常量?
作為從其他語言轉(zhuǎn)向ruby的,我發(fā)現(xiàn)ruby在一個class調(diào)用this
有如下幾種
self.xxxx
xxxx
方法@
這給我這種習(xí)慣了在java
, php
這類語言里只有一種方法引用類本身的人帶來了疑惑(當(dāng)然java也可以省略this),可以具體解釋下這三類用法的區(qū)別么?
我在很多rails項(xiàng)目發(fā)現(xiàn)了這種代碼
class Person
attr_accessor :name
def set_name(your_name)
name = your_name
end
end
def ... end
那個沒啥問題,但是前面的attr_accessor :name
怎么看怎么像一個正常的方法調(diào)用嘛,是這樣嗎?這里的方法跟一般的方法有啥不同?為啥要這樣寫。因?yàn)橐话愕恼Z言class里都只是聲明語法,雖然scala
之類的語言也可以執(zhí)行代碼,但不知道ruby的這種寫法有什么應(yīng)用場景。
學(xué)習(xí)是最好的投資!
attr_accessor
生成getter和setter方法。同樣的,attr_reader
生成getter,而attr_writer
Generate setter. In Ruby, it is common practice to execute special methods in a class. These methods are generally used to modify (or add) certain features of the class. 1.String is a variable in ruby ??and Symbol is a constant
2. 1 and 2 are the same. @ is a class member. It is very common in Ruby to provide multiple ways of calling a method
3. Equivalent to providing the default getter/setert method
Your question is contextual, I will reply backwards, you may understand it better :)
Execute the code and see the results. Just understand. attr_accessor is a method. After execution, the method will be dynamically added to the class.
This is metaprogramming in ruby. Very awesome said:
module Mod
attr_accessor(:one, :two)
end
Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
In the attr_accessor example just now, symbol (starting with colon) is used.
Why not use strings? Because many methods need to be generated using this method, and a method is a symbol, not a string. In more detail, I learned about the difference between symbol vs. string when I wrote my own Lisp interpreter. Because Ruby supports metaprogramming, it introduces some compiler concepts to enhance the flexibility of the language. This is really awesome.
That is to say, class methods and instance methods need to be distinguished. class method vs. member method .Many other languages ??also support this difference.
In short, class method can be called without instantiation, while member method must be instantiated before it can be called.
class foo def bar end end
foo.new.bar #調(diào)用實(shí)例方法,必須先new
class foo def self.bar end end
foo.bar #無需實(shí)例化
ref:http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/
Ruby’s methods are all called through an object, which is called the receiver of the method. The object’s methods are defined in the class to which the object belongs.
How to determine the receiver of a method? The following principles can be followed.
1. When called explicitly, the receiver is obviously the object you gave.
2. Implicitly called methods whose receiver is self.
3.self refers to:
(1) Within a module/class, self refers to the module/class (ruby modules/classes are also objects). Such as
class MyClass
self #Here self is the MyClass object
end
(2) Inside the method, self refers to the receiver of the method. Such as
def method
self #Here self is the receiver used when calling this method
#It is determined when the method is called
end
The ones starting with @ are instance variables, which are two different things from methods.
There is so much knowledge about Ruby that it is difficult to explain clearly in a few words. What is mentioned here is just the tip of the iceberg. Ruby has a very rigorous and complete logic system.
attr_accessor is a statement
Please finish learning the grammar first