如何让课程互相交流

时间:2016-04-25 06:39:52

标签: ruby

我有班人,狗,猫和鱼,我需要他们互动。更具体地说,我需要Person类来购买dog类的实例,或者命名cat类的实例等。

我该怎么做?我的变量是类变量,它们超出了在另一个类中使用的范围。我不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

Let's say you have person.rb:

Class Person
 .......
end

and you have dog.rb:

class Dog
 ........
end

If you want to access to Dog class inside Person you have to include it. That means your dog.rb would be:

require 'dog'

class Person
  def my_method
    dog = Dog.new()
  end
end

require 'name_of_file_with_class'

This is just a pure example how to make it accessible, furthermore it depends on what exactly do you want.

Cheers.