Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a static method in a Ruby module from a class that includes the module?

Tags:

ruby

Is it possible to declare static methods in a module in ruby?

module Software   def self.exit     puts "exited"   end end  class Windows   include Software    def self.start     puts "started"     self.exit   end end  Windows.start 

The example above will not print out "exited".

Is it only possible to have instance methods in a module?

like image 479
never_had_a_name Avatar asked Jul 28 '10 21:07

never_had_a_name


People also ask

How do you call a method inside a module in Ruby?

As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.

How do you call a static method from a class?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

How do you call a static method from an object?

To call a static method outside of the class that defines it we can use the class name such as Example. printMessage();.

Can I call a static method inside a regular method?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one.


2 Answers

Define your module like this (i.e. make exit an instance method in the module):

module Software   def exit     puts "exited"   end end 

and then use extend rather than include

class Windows   extend Software   # your self.start method as in the question end 

In use:

irb(main):016:0> Windows.start started exited => nil 

Explanation

obj.extend(module, ...) adds to obj the instance methods from each module given as a parameter

...so when used within the context of a class definition (with the class itself as the receiver) the methods become class methods.

like image 64
mikej Avatar answered Sep 19 '22 11:09

mikej


Put your class methods in a nested module, and then override the "included" hook. This hook is called anytime your module is included. Inside the hook, add the class methods to whomever did the include:

module Foo    def self.included(o)     o.extend(ClassMethods)   end    module ClassMethods      def foo       'foo'     end    end  end 

Now any class including Foo gets a class method named foo:

class MyClass   include Foo end  p MyClass.foo    # "foo" 

Any non-class methods may be defined in Foo as usual.

like image 43
Wayne Conrad Avatar answered Sep 18 '22 11:09

Wayne Conrad