Ruby rediscovery classes - can I access overridden methods?

I know that if I subclass the String class and override its capize method, I can name the caption class of the String string with super. What if, instead, I reopened the String class and rewrote the capize method? Is there a way that I can call a previous version of this method?

+5
source share
2 answers

Not out of the box. The general approach is to rename an existing method to a new name. Then, in your overwritten version, call the old method with the new name.

def String
    alias to_i old_to_i
    def to_i
       #add your own functionality here
       old_to_i
    end
end

You can also see alias_method_chainwhat does this for you.

+5

super - (, ):

, , . , ( super).

, , irb- ripl, ( super, ).

0

All Articles