Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - overwrite def method

Tags:

object

class

ruby

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.

like image 291
Puru puru rin.. Avatar asked Jul 10 '26 19:07

Puru puru rin..


1 Answers

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.

like image 198
alex.zherdev Avatar answered Jul 17 '26 21:07

alex.zherdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!