如果self是ruby中的默認接收方,並且您在實例方法定義中調用'puts',則該對象的實例是該調用的接收方?紅寶石自己放
E.g.
class MyClass
attr_accessor :first_name, :last_name, :size
# initialize, etc (name = String, size = int)
def full_name
fn = first_name + " " + last_name
# so here, it is implicitly self.first_name, self.last_name
puts fn
# what happens here? puts is in the class IO, but myClass
# is not in its hierarchy (or is it?)
fn
end
end
非常好的一點。這裏有什麼協議,我應該刪除我的答案並留下你的答案(因爲它更準確)? – 2012-03-28 16:17:17
其實我錯了。 'self.puts'不同於在當前類中調用'puts'方法。因此,我的答案的第一個修訂版本基本上與您的說法相同,是對的,您的答案也是如此。 – 2012-03-28 18:05:38