我有以下結構如何從另一個類中調用另一個類的方法?
class A
def method1
end
end
class B
@my = A.new
def classATest
@myT.method1
end
def newTest
classATest
end
end
class C
newB = B.new
newB.newTest
end
當我運行的C類,它給我的錯誤,它找不到類的方法1(方法newtest,調用方法classATest,它調用使用全局變量的方法1。我究竟做錯了什麼?這難道不是可以嗎?
Ruby的一個慣例是使用* snake-case *作爲方法和變量的名字,這意味着你可以寫'new_test'和'class_a_test'(或者可能'classA_test')。你不需要那樣做,但我們99%的人都這麼做。當你有一些時間的時候,你可能想看看[Ruby Style Guide](https://github.com/styleguide/ruby/)。 –