在下面的代碼示例中,爲什麼第二個(sheetplus)方法似乎可以獲取實例變量@name和@occupation,但第一個(sheet)方法返回nil?我覺得我錯過了致命的明顯的東西,但我基本上是世界上最糟糕的Ruby程序員。在類方法中,爲什麼一個方法似乎接受實例變量,而另一個方法卻不接受?
class Test
def initialize(name, occupation)
@name = name
@occupation = occupation
def sheet
"This is #@name, who is a/an #@occupation"
def sheetplus
"This is #@name, who is a/an #@occupation, but why does this method succeed where the previous one fails?"
end
end
end
end
使用「Tab」鍵! – 2010-07-19 03:03:18