I
I
Ivan Nesterovich2014-07-14 15:42:20
Ruby on Rails
Ivan Nesterovich, 2014-07-14 15:42:20

[Ruby on Rails] how to override parent method?

I want to patch up one method in the gem to fit my needs. I have little experience in OOP and do not quite understand how to override the parent method so that when this method is called, mine is called? Or is it just forking the gem and editing directly?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
OnYourLips, 2014-07-14
@OnYourLips

You can rediscover the class in your code and rewrite the method.
But this is the wrong approach.

V
Viktor Vsk, 2014-07-14
@viktorvsk

This method is probably in some module.
As an idea, you can do:

module GemModule do
class GemModuleCass << SomeOtherClass

def method
...
end

end

or
GemModule::GemClass.class_eval do
def method
...
end
end

@
@proffard, 2014-07-14
_

If ruby ​​> 2.0, then it's better to use refine.
here is a good guide on how to do it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question