The Difference Between instance_eval and class_eval in Ruby

Written by on Aug 1 2012

The Ruby methods instance_eval and class_eval seem to be really straight forward, until you start using them. Then you realize that the whole world seems ass backwards. Let’s look at an example:

String.instance_eval do
  def from_instance_eval
    self
  end
end

String.class_eval do
  def from_class_eval
    self
  end
end

p String.from_instance_eval      #String
p "string".from_class_eval       #"string"

begin
  String.from_class_eval
rescue Exception  => e
  p e  
  # => #<NoMethodError: undefined method `from_class_eval' for String:Class>
end

begin
  "string".from_instance_eval
rescue Exception  => e
  p e                            
  # => #<NoMethodError: undefined method `from_instance_eval' for "string":String>
end

The first conclusion to be drawn from this is that instance_eval is for classes and class_eval is for instances, which creates a “WTF?!” moment. But don’t worry, this isn’t quite the right way of looking at it. Let’s see what happens when we call the eval methods on an instance of an object:

"string2".instance_eval { p self }   
# => "string2"
begin
  "string2".class_eval { p self }
rescue Exception  => e
  p e                               
  # => #<NoMethodError: undefined method `class_eval' for "string2":String>
end

So what have we discovered? We can use instance_eval on any object, even class objects (remember in Ruby everything is an object, even classes are objects). Because it’s eval-ing on the receiver, if it’s used on an instance of an object then obviously it evals on that instance creating an instance method. And if it’s used on a class object then, again obviously, it evals on the class creating a class method. (I say obviously a bit tongue in cheek here.)

On the other hand class_eval can only be called on classes or modules and it evals on the instances of the class/module creating instance methods.

Instance of an object Class
instance_eval Acts on the receiver (self), the instance. Acts on the receiver (self), the class.
class_eval Not Available Acts on any instance of the class.

P.S. module_eval is exactly the same as class_eval. Either can be used for modules or classes.

Meet
Steven

Hi I'm Steven,

I wrote the article you're reading... I lead the developers, write music, used to race motorcycles, and help clients find the right features to build on their product.

Get Blog Updates