Puru puru rin.. Puru puru rin.. - 6 months ago 29
Ruby Question

Ruby - overwrite def method

How can I overwrite the def method? But it's strange, cause I don't know from where the def method is defined. It's not Module, not Object, not BasicObject (of Ruby 1.9). And def.class don't say nothing ;)

I would like to use something like:

sub_def hello
puts "Hello!"
super
end

def hello
puts "cruel world."
end

# ...and maybe it could print:
# => "Hello!"
# => "cruel world."


Many thanks, for any ideas.

Answer

Who told you def is a method? It's not. It's a keyword, like class, if, end, etc. So you cannot overwrite it, unless you want to write your own ruby interpreter.

Comments