Puru puru rin.. Puru puru rin.. - 9 months ago 75
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!"

def hello
puts "cruel world."

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

Many thanks, for any ideas.


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.