Can we modify the method defination once it is defined inside a class?

Hi,

Suppose I have written the below code:

class Foo

def show

p “Good”

end

end

Now is it possible in future to re-open the “show” method again to add
some new logic,once the class Foo has been defined ?

look for alias and alias_method

Hello Xavier,

yes, it is possible. There are three options you have, depending on what
you want:

  • Inheritance: Overwrite the method show in a subclass and then call
    super (if you need the new behavior independently of the old behavior)
  • Alias the old method show to something like “old_show” and then
    redefine show (if you want to call the old method); alias_method and
    alias are the tools for that
  • Just redefine the method (if you don’t need the old method)

Kind regards,

Calvin