在 Ruby 中调用另一个类中的类中的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31360945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:45:28  来源:igfitidea点击:

Call a method in a class in another class in Ruby

rubyclassmethodsundefined

提问by Pabi

I was wondering how I could call a method in an instance of a class in another class.

我想知道如何在另一个类的类的实例中调用方法。

This is what I came up with

这是我想出的

class ClassA
  def method
    return "This is a method_from_class_A"
  end
end

class ClassB
  def initialize
    @method_from_class_A=instance.method
  end
  def method_calls_method
    @method_from_class_A
  end
end


instance=ClassA.new

instance2=ClassB.new

puts instance2.method_calls_method

But I get this error:

但我收到此错误:

Testing.rb:9:in initialize': undefined local variable or method instance' for # (NameError) from Testing.rb:19:in new' from Testing.rb:19:in'

Testing.rb:9:in initialize': undefined local variable or method instance' for # (NameError) from Testing.rb:19:in new' from Testing.rb:19:in'

How could I fix it?

我怎么能修好呢?

Thank you for your response.

谢谢您的答复。

采纳答案by Mori

From your description this seems to be what you're going for:

从你的描述来看,这似乎是你想要的:

class ClassB
  def initialize
    @instance_of_class_a = ClassA.new
  end

  def method_calls_method
    @instance_of_class_a.method
  end
end

Or to pass in the ClassAinstance (this is called dependency injection):

或者传入ClassA实例(这称为依赖注入):

class ClassB
  def initialize(class_a_instance)
    @instance_of_class_a = class_a_instance
  end

  def method_calls_method
    @instance_of_class_a.method
  end
end

instance_a = ClassA.new
instance_b = ClassB.new(instance_a)
puts instance_b.method_calls_method

回答by timthez

Another Option would be to take a look at class methods: https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/113-class-variables

另一种选择是查看类方法:https: //rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/113-class-variables

So in your code it would look similar to this:

因此,在您的代码中,它看起来类似于:

class ClassA
  def self.method
    return "This is a method_from_class_A"
  end
end

class ClassB
  def method_calls_method
    ClassA.method
  end
end


instance=ClassB.new

puts instance.method_calls_method

*Notice the self.in ClassA to signify a class method. This is similar to a static method in other languages.

*注意self.ClassA 中的 表示类方法。这类似于其他语言中的静态方法。

According to wikipedia: https://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods

根据维基百科:https://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods

Class(static) methods are meant to be relevant to all the instances of a class rather than to any specific instance.

类(静态)方法旨在与类的所有实例相关,而不是与任何特定实例相关。

You see class methods used a lot in the ruby Math class: http://ruby-doc.org/core-2.2.2/Math.html

您会看到 ruby​​ Math 类中大量使用的类方法:http: //ruby-doc.org/core-2.2.2/Math.html

For example taking a square root of a number in is done by using the class method Math.sqrt. This is different from an instance method which would look like object.methodinstead Class.method. There are a lot of resources and tutorials out that explains this concept in more detail and probably clearer.

例如,使用类方法对 in 取一个数的平方根Math.sqrt。这是一个实例方法,它看起来像不同的object.method代替Class.method。有很多资源和教程更详细地解释了这个概念,可能更清楚。