将模块方法调用到Ruby中的另一个模块中

时间:2022-06-24 23:20:24

FIle module.rb

文件module.rb

module CardExpiry
  def check_expiry value
    return true
  end
end

file include.rb

文件include.rb

#raise File.dirname(__FILE__).inspect
require "#{File.dirname(__FILE__)}/module.rb"

 module Include
     include CardExpiry
  def self.function 
    raise (check_expiry 1203).inspect
  end
end

calling

调用

Include::function

is this possible ?

这是可能的吗?

Error trigger when calling :

调用时错误触发:

`function': undefined method `check_expiry' for Include:Module (NoMethodError)

2 个解决方案

#1


6  

You stumbled over the difference of include and extend.

你在包含和扩展的差异上犯了错误。

  • include makes the method in the included module available to instances of your class
  • 包括使包含的模块中的方法可用于类的实例。
  • extend makes the methods in the included module available in the class
  • 扩展使所包含模块中的方法在类中可用

When defining a method with self.method_name and you access self within that method, self is bound to the current class.

用self定义方法时。method_name和您在该方法中访问self, self被绑定到当前类。

check_expiry, however, is included and thus only available on the instance side.

但是,包含check_expire,因此只能在实例端可用。

To fix the problem either extend CardExpiry, or make check_expiry a class method.

要解决这个问题,可以扩展CardExpiry,或者将check_expiration设置为类方法。

#2


0  

I've looked at your problem in a bit more detail, and the issue is your module.rb file:

我已经详细地研究了您的问题,问题是您的模块。rb文件:

module CardExpiry
  def self.check_expiry value
    return true
  end
end

First, there was an end missing from the file - both def and module need to be closed.

首先,文件中缺少一个结束——def和模块都需要关闭。

Second, the magical self. in the def line turns the method into a pseudo-global function - this answer explains it better than I can.

第二,神奇的自我。在def行中,将方法转换为伪全局函数——这个答案比我能更好地解释它。

Furthermore, to call the method, you need to use:

此外,要调用该方法,需要使用:

raise (CardExpiry::check_expiry 1203).inspect

#1


6  

You stumbled over the difference of include and extend.

你在包含和扩展的差异上犯了错误。

  • include makes the method in the included module available to instances of your class
  • 包括使包含的模块中的方法可用于类的实例。
  • extend makes the methods in the included module available in the class
  • 扩展使所包含模块中的方法在类中可用

When defining a method with self.method_name and you access self within that method, self is bound to the current class.

用self定义方法时。method_name和您在该方法中访问self, self被绑定到当前类。

check_expiry, however, is included and thus only available on the instance side.

但是,包含check_expire,因此只能在实例端可用。

To fix the problem either extend CardExpiry, or make check_expiry a class method.

要解决这个问题,可以扩展CardExpiry,或者将check_expiration设置为类方法。

#2


0  

I've looked at your problem in a bit more detail, and the issue is your module.rb file:

我已经详细地研究了您的问题,问题是您的模块。rb文件:

module CardExpiry
  def self.check_expiry value
    return true
  end
end

First, there was an end missing from the file - both def and module need to be closed.

首先,文件中缺少一个结束——def和模块都需要关闭。

Second, the magical self. in the def line turns the method into a pseudo-global function - this answer explains it better than I can.

第二,神奇的自我。在def行中,将方法转换为伪全局函数——这个答案比我能更好地解释它。

Furthermore, to call the method, you need to use:

此外,要调用该方法,需要使用:

raise (CardExpiry::check_expiry 1203).inspect