Generic-user-small James Whiteman 4 posts

Hello, all.

Does anyone have a good explanation for this?


class Foo
end

Foo.instance_eval do
  def jump
    "jumping..." 
  end

  define_method(:shout) do
    "shouting..." 
  end
end

Foo.respond_to? :jump
  => true
Foo.respond_to? :shout
  => false
Foo.new.respond_to? :shout
  => true

It seems that define_method is pushing the method ‘shout’ into Foo’s own m_tbl instead of in the metaclass.
This was a little unexpected to me.

 
Dave_8_trans_small Dave Thomas Administrator 65 posts

define_method always creates an instance method—now idea why, but it’s a deliberate decision of the implementers.

Dave

 
Generic-user-small James Whiteman 4 posts

Thanks, Dave—fantastic series by the way.

3 posts, 2 voices