Home  >  Q&A  >  body text

一些关于ruby的初学者问题

我是因为用了coffee所以萌生出想学学ruby的想法的,因为才刚开始学习,所以有些问题可能比较初级。

Symbol对象的本质是什么?

我看的代码里大量使用了:xxxx之类的语法,我从网上查到这个叫Symbol,从用法上来看它的本质应该是不是一种不需要预定义,但是会被预编译的常量

在Class里指向自身?

作为从其他语言转向ruby的,我发现ruby在一个class调用this有如下几种

  1. 使用self.xxxx
  2. 直接调用当前类定义的xxxx方法
  3. 使用@

这给我这种习惯了在java, php这类语言里只有一种方法引用类本身的人带来了疑惑(当然java也可以省略this),可以具体解释下这三类用法的区别么?

在Class里直接写代码?

我在很多rails项目发现了这种代码

class Person
  attr_accessor :name

  def set_name(your_name)
    name = your_name 
  end
end

def ... end那个没啥问题,但是前面的attr_accessor :name怎么看怎么像一个正常的方法调用嘛,是这样吗?这里的方法跟一般的方法有啥不同?为啥要这样写。因为一般的语言class里都只是声明语法,虽然scala之类的语言也可以执行代码,但不知道ruby的这种写法有什么应用场景。

PHPzPHPz2761 days ago1021

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-04-22 09:01:56

    1. Symbol is a basic type (also a class) in Ruby. It is a constant and its usage is similar to that of an enumeration type
    2. Methods 1 and 2 are equivalent because self refers to this class at this time. Method 1 is generally used because it can avoid the problem of modification when renaming the class, and it can be added to the snippet.
    3. attr_accessor生成getter和setter方法。同样的,attr_reader生成getter,而attr_writerGenerate 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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-22 09:01:56

    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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-22 09:01:56

    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:

    module Mod
      attr_accessor(:one, :two)
    end
    Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]
    

    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.

    class foo def bar end end
    foo.new.bar #调用实例方法,必须先new
    
    class foo def self.bar end end 
    foo.bar #无需实例化
    
    ref:http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/
    

    reply
    0
  • 高洛峰

    高洛峰2017-04-22 09:01:56

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-22 09:01:56

    attr_accessor is a statement
    Please finish learning the grammar first

    reply
    0
  • Cancelreply