Strange behavior when class_eval'ing a Class object

Just for the sake of learning more about the Ruby object model, I’ve
been doing some experiments, and the following behavior surprised me:

Let’s say we have a class Test:

class Test;end

Then, we class_eval Test.class in order to add a method to the Test

Test.class.class_eval do
def foo
puts ‘foo called!’

(equivalent to calling Test.class.send(:define_method,:foo) … )


=> foo called!

But sending foo to class also works:

=> foo called!

I still can’t figure out why, though. The Class’ singleton_class doesn’t
contain it (the only possible explanation that came to my mind was that
somehow the method was added to the Class’ singleton class):

=> [:nesting, :constants]

How does the method lookup work in this case? Why does sending :foo to
Test.class also calls the method ? Am I missing something ?

As a reference, I did the same thing with an instance of Test:

irb(main):001:0> class Test;end
=> nil
irb(main):002:0> _foo =
=> #Test:0x007fa2c39e2f38
irb(main):003:0> _foo.class.class_eval do
irb(main):004:1* def foo
irb(main):005:2> puts ‘foo called!’
irb(main):006:2> end
irb(main):007:1> end
=> :foo
foo called!
=> nil
NoMethodError: undefined method `foo’ for Test:Class

This worked the way I expected it to.

Thanks in advance!

Because Test.class == Class and Class.class == Class

It’s kind of swirly.

class A
def foo()…;end
B.is_a? A # so works
C.is_a? A # so works


class Class
def foo()…;end
Test.is_a? Class # works
Class.is_a? Class # works

Matthew K.

Yep! I just figured out that a few minutes before you sent the message!
Eureka :slight_smile:

Thank you very much for taking the time to answer.


Marcelo Serpa
Sent with Sparrow (

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs