亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

一些關(guān)于ruby的初學(xué)者問題
PHPz
PHPz 2017-04-22 08:59:56
0
5
1125

我是因?yàn)橛昧薱offee所以萌生出想學(xué)學(xué)ruby的想法的,因?yàn)椴艅傞_始學(xué)習(xí),所以有些問題可能比較初級。

Symbol對象的本質(zhì)是什么?

我看的代碼里大量使用了:xxxx之類的語法,我從網(wǎng)上查到這個叫Symbol,從用法上來看它的本質(zhì)應(yīng)該是不是一種不需要預(yù)定義,但是會被預(yù)編譯的常量?

在Class里指向自身?

作為從其他語言轉(zhuǎn)向ruby的,我發(fā)現(xiàn)ruby在一個class調(diào)用this有如下幾種

  1. 使用self.xxxx
  2. 直接調(diào)用當(dāng)前類定義的xxxx方法
  3. 使用@

這給我這種習(xí)慣了在java, php這類語言里只有一種方法引用類本身的人帶來了疑惑(當(dāng)然java也可以省略this),可以具體解釋下這三類用法的區(qū)別么?

在Class里直接寫代碼?

我在很多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)用場景。

PHPz
PHPz

學(xué)習(xí)是最好的投資!

reply all(5)
Peter_Zhu
  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.
巴扎黑

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:

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 #調(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.

Peter_Zhu

attr_accessor is a statement
Please finish learning the grammar first

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template