You can use Module as a helper to store common methods to be shared across different classes.

By including the module in your class, you inherit its methods. But, problem pops up, when on some cases you need to call the helper method from a class mehod instead of an instance method. And you know you can’t call an instance method directly from a class method.

So this was what I did.

module Helper
    self.included(base)
        base.extend(self)
    end

    def hello
        "Hello world"
    end
end

class Service
    include Helper

    def instance_call
       hello
    end

    def self.class_call
       hello
    end
end

Service.new.instance_call # returns 'hello world'
Service.class_call        # returns 'hello world'

No Comments . Comments Feed . Trackback URI

Add Your Comment



(optional)