我有以下结构如何从另一个类中调用另一个类的方法?
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/)。 –