CodeRead: metaclass gem: Accessing An Object’s Singleton Class

less than 1 minute read

In ruby, an object is responsible for holding it’s data attributes.

o =
o.instance_variable_set "@name", "box"
o # #<Object:0x007fbc7912d5a8 @name="box">

But it’s not responsible for holding it’s methods. An object’s methods reside in its defining class. And if an object has a specific method just for itself, it’s held in the object’s singleton class.

class Dummy
  def say_hello

d =
p d.methods.grep /say_hello/  # [:say_hello]
p Dummy.instance_methods.grep /say_hello/  # [:say_hello]

sc = class << d; self; end

sc.class_eval { def say_hi; 'hi'; end }

p d.say_hi  # 'hi'

p d.methods.grep /say_hi/  # [:say_hi']
p Dummy.instance_methods.grep /say_hi/  # []

In the last line, note that the say_hi method isn’t seen in Dummy’s list of instance methods.

The metaclass gem

This gem gives easy access to an object’s metaclass (same as singleton class) through a __metaclass__ method. It’s implementation is easy.

Dummy = do
  def __metaclass__
    class << self

obj =

obj.__metaclass__.class_eval { def only_me; 'Yes You'; end }

obj2 =

p obj.only_me # 'Yes You'