xeno
xeno

Reputation: 3

How to call a method immediately after initialization in ruby?

Let's say I have:

module Something
  class SomethingElse

    def initialize(args)
      @args = args
    end

    def some_method
      #stuff
    end
  end
end

Is there a way I can get some_method to run automatically right after intialize, but without calling some_method from within the initialize method?

Upvotes: 0

Views: 492

Answers (1)

sawa
sawa

Reputation: 168249

Yes, if you allow to also define initialize in another module.

module Child
  def initialize
    super
    # ... some_method_stuff
  end
end

Something.prepend Child

Upvotes: 2

Related Questions