I'm learning ruby metaprogramming at the moment and while I was playing around and testing stuff, I stumbled upon something I can't seem to find the answer to. Let's say we have the following code:
class Foo
end
Foo.instance_eval do
define_method("bar") do
1
end
end
I would expect this to add a class method called bar
to Foo
but instead when I call the method it says it's undefined. What baffles me even more is that the same code works when I use def
instead of define_method
. Both ways seem to work when I try to define an instance method with class_eval
as well. So what's really going on here?
Thanks in advance.