我不知道如何用文字解释它,所以让我在代码中展示它:
module Foo # "default method" def self(string) "It's a #{string}" end def self.add(a, b) a + b end end Foo.add(10, 5) # => 15 Foo('test') # => It's a test
Ruby中有可能这样吗?谢谢.
不,但您可以定义与模块或类相同的方法:
module Foo def self.add(a, b) a + b end end def Foo(string) "It's a #{string}" end Foo.add(10, 5) # => 15 Foo('test') # => It's a test