我是因为用了coffee所以萌生出想学学ruby的想法的,因为才刚开始学习,所以有些问题可能比较初级。
我看的代码里大量使用了:xxxx
之类的语法,我从网上查到这个叫Symbol
,从用法上来看它的本质应该是不是一种不需要预定义,但是会被预编译的常量?
作为从其他语言转向ruby的,我发现ruby在一个class调用this
有如下几种
self.xxxx
xxxx
方法@
这给我这种习惯了在java
,php
这类语言里只有一种方法引用类本身的人带来了疑惑(当然java也可以省略this),可以具体解释下这三类用法的区别么?
我在很多rails项目发现了这种代码
class Person attr_accessor :name def set_name(your_name) name = your_name end end
def ... end
那个没啥问题,但是前面的attr_accessor :name
怎么看怎么像一个正常的方法调用嘛,是这样吗?这里的方法跟一般的方法有啥不同?为啥要这样写。因为一般的语言class里都只是声明语法,虽然scala
之类的语言也可以执行代码,但不知道ruby的这种写法有什么应用场景。
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 :)
3. attr_accessor
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:
1. symbol
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.
2. Self is equivalent to calling a method directly, both are calling member methods. What is called with @ is the class method.
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.
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