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?
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.
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.
To call a static method outside of the class that defines it we can use the class name such as Example. printMessage();.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With