我想知道如果一個類或任何變量被鏈接到一個或多個方法?它會給予哪個回報值? (Class.method1.method2
- >什麼是返回值?)當方法鏈接時,如何確定將返回哪個返回值?
下面給出了下面的代碼:
restaurants = []
restaurants << Restaurant.new.import_line(line.chomp)
def import_line(line)
return self
end
在這種情況下,這兩種方法(new
和import_line(line)
返回實例,所以我知道的情況下被放入陣。我很好奇,但如果import_line(line)
,返回類似true
def import_line(line)
return true
end
什麼是對Restaurant.new.import_line(line.chomp)
返回值。它會被設置爲true
或返回實例(.new
返回實例)?
實質上,當存在Class.method1.method2
或Variable.method1.method2
時,哪種方法在確定返回值時具有優先權?
任何幫助將不勝感激!
@tadman很好的回答。更進一步,當你執行'object.method1.method2.method3'時,'object.method1'的結果成爲調用'method2'的對象,其結果成爲'method3'的對象被調用,其結果返回給原來的調用者。 – moveson
優秀的分析@moveson和謝謝你tadman。 – the12