在模块中定义类方法

时间:2023-01-15 18:42:36

I mean, I want to create a class method in my module that will be used by the class who includes the module. They are in separated files.

我的意思是,我想在我的模块中创建一个类方法,该方法将由包含该模块的类使用。它们位于单独的文件中。

By far I have something like this:

到目前为止,我有这样的事情:

module Base
  def self.all
    puts "All Users"
  end
end

class User
  include Base
end

But I'm getting: NoMethodError: undefined methodall' for User:Class`

但我得到:NoMethodError:未定义的方法'用户:Class`

Can you please explain the problem and if what I'm doing is a bad practice or going against any principle?

你能解释一下这个问题吗?如果我正在做的是一个不好的做法或违反任何原则?

2 个解决方案

#1


3  

You can extend the module in your class, your code should be like this:

您可以在类中扩展模块,您的代码应该是这样的:

module Base
  def all
    puts "All Users"
  end
end

class User
  extend Base
end

When you do something like this:

当你做这样的事情时:

module MyModule
  def self.module_method
    puts "module!"
  end
end

You're actually adding the method in the module itself, you could call the previous method like this:

您实际上是在模块本身中添加方法,您可以像这样调用上一个方法:

MyModule.module_method

There is a way to just include the module and get the behaviour that you want, but I don't think this could be considered the "way to go". Look at the example:

有一种方法可以只包含模块并获得您想要的行为,但我不认为这可以被视为“走的路”。看看这个例子:

module Base
  def self.included(klass)
    def klass.all
      puts "all users"
    end
  end
end

class User
  include Base
end

But like I said, if you can go with the extend class method, it is a better suit.

但就像我说的,如果你可以使用扩展类方法,它是一个更好的诉讼。

#2


0  

Following Ricardo's answer, consider an idiom common among Ruby programmers - enclose your module's class methods into inner module, called ClassMethods(that's a mouthful, I know), and use and Module#included hook to extend base class with ClassMethods module.

根据里卡多的回答,考虑一下Ruby程序员常用的习惯用法 - 将你的模块的类方法包含在内部模块中,称为ClassMethods(我知道这是一口),并使用Module #include钩子来扩展基类和ClassMethods模块。

More information here: http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

更多信息请访问:http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

#1


3  

You can extend the module in your class, your code should be like this:

您可以在类中扩展模块,您的代码应该是这样的:

module Base
  def all
    puts "All Users"
  end
end

class User
  extend Base
end

When you do something like this:

当你做这样的事情时:

module MyModule
  def self.module_method
    puts "module!"
  end
end

You're actually adding the method in the module itself, you could call the previous method like this:

您实际上是在模块本身中添加方法,您可以像这样调用上一个方法:

MyModule.module_method

There is a way to just include the module and get the behaviour that you want, but I don't think this could be considered the "way to go". Look at the example:

有一种方法可以只包含模块并获得您想要的行为,但我不认为这可以被视为“走的路”。看看这个例子:

module Base
  def self.included(klass)
    def klass.all
      puts "all users"
    end
  end
end

class User
  include Base
end

But like I said, if you can go with the extend class method, it is a better suit.

但就像我说的,如果你可以使用扩展类方法,它是一个更好的诉讼。

#2


0  

Following Ricardo's answer, consider an idiom common among Ruby programmers - enclose your module's class methods into inner module, called ClassMethods(that's a mouthful, I know), and use and Module#included hook to extend base class with ClassMethods module.

根据里卡多的回答,考虑一下Ruby程序员常用的习惯用法 - 将你的模块的类方法包含在内部模块中,称为ClassMethods(我知道这是一口),并使用Module #include钩子来扩展基类和ClassMethods模块。

More information here: http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

更多信息请访问:http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/