English 中文(简体)
模块功能有可能从单元内也包含在该单元的班级内。
原标题:Is it possible to call a Module function from inside a class that is also in that module
  • 时间:2010-07-01 16:37:39
  •  标签:
  • ruby

在该法典中:

Module M
  Class C < Struct.new(:param)
    def work
      M::helper(param)
    end
  end

  def helper(param)
    puts "hello #{param}"
  end
end

我在试图操作M:Module的“不确定方法助手”错误

c = M::C.new("world")
c.work

但是,打电话M:helper ("world”),直接来自另一类作品。 班级是否不使用同一单元界定的单元功能? 除了将这一类移出模块之外,是否还有其他办法?

最佳回答

In order to invoke M::helper you need to define it as def self.helper; end For the sake of comparison, take a look at helper and helper2 in the following modified snippet

module M
  class C < Struct.new(:param)
    include M     # include module to get helper2 mixed in
    def work
      M::helper(param)
      helper2(param)
    end
  end

  def self.helper(param)
    puts "hello #{param}"
  end

  def helper2(param)
    puts "Mixed in hello #{param}"
  end
end

c = M::C.new("world")
c.work
问题回答

您应使用<代码>自行的预支模块方法:

module M
  class C < Struct.new(:param)
    def work
      M::helper(param)
    end
  end

  def self.helper(param)
    puts "hello #{param}"
  end
end

<代码>C 试图在<代码>M上打电话<编码>。 www.un.org/Depts/DGACM/index_spanish.htm M 单吨级。 另外,你也说,<代码>求助器是一种模块功能,只有一种方法。 a 模块功能将使代码发挥作用:

module M
  class C < Struct.new(:param)
    def work
      M::helper(param)
    end
  end

  module_function

  def helper(param)
    puts "hello #{param}"
  end
end

将单元纳入该课程还将发挥作用:

module M
  class C < Struct.new(:param)
    include M

    def work
      helper(param)
    end
  end

  def helper(param)
    puts "hello #{param}"
  end
end

在第一个例子中,<条码>求助器载于<条码>。 第二例进口<代码>Ms 载于<编码>C。 因此,<代码>C可加以使用。 另一个不同之处是,首先,你可以在你的法典中从任何地方打电话M.helper。 在第二种情况下,您将能够在您的法典中从<代码>C的任何方面打上。 为确定这一点,将<代码> 求助代码 私人:

module M
  class C < Struct.new(:param)
    include M

    def work
      helper(param)
    end
  end

  private
  def helper(param)
    puts "hello #{param}"
  end
end

以下是一些解释:

http://ruby-doc.org/french/ga/president

模块是一套方法和固定单元。 模块中的方法可以是实例方法或模块方法。 实例方法在列入模块时作为类别的方法出现,模块方法没有。 反之,可使用模块方法而不必形成一个概括性物体,而实例方法则不得使用。 (见模块#module_Function.)

<代码>本身.methodname within a model establishs a model methods.

在此情况下,您援引了M:helper。 页: 1 当你从C++制定者的角度看待这一问题时。 在这种情况下,接收器是模块标的(一种碎片构件)。


探讨这一问题的其他途径是了解接收人的概念,每一种方法都包含接收人和方法名称(+选择性的寄生虫和编码组)。 接收器可以是<条码>。 类别物体:或用户定义类别的例子。

只有在单元(或班级)物体上才能使用单元(或班级)方法。 你可以援引任何方法(摩迪勒/地图/内容)。

如果你想采用模块中界定的某种审查方法,那么你就必须通过<条码>(包括)给它一个接收人,该模块在某些类别中设置,并形成一个实例。

因此,另一种解决办法是:

module MM
  def helper(param)
    puts "hello #{param}"
  end

  class ReceiverClass
    include MM           # add helper() to ReceiverClass
  end

  class C < Struct.new(:param)
    def work
      ReceiverClass.new.helper(param)
    end
  end
end

c = MM::C.new("world")
c.work




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签