Fun with Ruby’s instance_eval and class_eval

In an attempt to better understand instance_eval and class_eval, I just read Khaled’s post on Ruby reflection. It helped, and I came up with a memory crutch I can use to remember when to use each of them:

Use ClassName.instance_eval to define class methods.

Use ClassName.class_eval to define instance methods.

That’s right. Not a typo. Here are some examples, shamelessly stolen from his post:

# Defining a class method with instance_eval
Fixnum.instance_eval { def ten; 10; end }
Fixnum.ten #=> 10

# Defining an instance method with class_eval
Fixnum.class_eval { def number; self; end }
7.number #=> 7

I Like Stuff that’s Backwards

Why is it the reverse of what you might expect? Because Fixnum.instance_eval treats Fixnum as an instance (an instance of the Class class), thus any new functions you define can be called on that instance. So it’s equivalent to this:

class Fixnum
  def self.ten
    10
  end
end
Fixnum.ten #=> 10

Fixnum.class_eval treats Fixnum as a class and executes the code in the context of that class, thus any “def” statements are treated exactly as if they were in normal code without any reflection. It’s equivalent to this:

class Fixnum
  def number
    self
  end
end
7.number #=> 7

There are still some things about Ruby reflection that mystify me but at least I think I’ve got this one nailed.

15 thoughts on “Fun with Ruby’s instance_eval and class_eval”

  1. The Pragmatic Programmers have a “Ruby Metaprogramming” screencast series ($$) that delves deeply into these and many more Ruby “mysteries”–I recommend them!

  2. Pingback: The "Tech. Arch."
  3. Brian, I have to disagree. instance_eval works on the particular instance. Fixnum here is an object, an instance, in particular an object of type Class. When you add methods to an instance, it only affects the instance. Hence, you get class methods when you apply instance_eval to an instance of Class. using class_eval is simply like opening up the Fixnum class and adding methods, so any all instances of Fixnum get instance methods themselves. If you take an instance of Fixnum and apply instance_eval only that instance would get the method, so you could Fixnum.new.instance_eval so you could dynamically extend a single instance of type Fixnum without affecting other instances.

    1. Thanks for the comments, Andy. You said you disagree but I think we actually agree with how instance_eval and class_eval work. I was trying to say exactly what you said. Sorry if it wasn’t clear.

  4. I guess more to the point, I find the mnemonic misleading. I don’t find it backwards, if you take the perspective that everything in Ruby is an object.

    1. I agree that it’s not backward if you take the perspective that everything is an object. That’s why I tried to explain it by saying Fixnum.instance_eval treats Fixnum as an instance of the Class class. It seems like it helped a bunch of the other commenters to remember not only which method is which but to actually understand why.

      But for those to whom the rationale still doesn’t make sense, I think there’s nothing wrong with a good memory crutch once in a while.

Leave a Reply

Your email address will not be published. Required fields are marked *

Feel free to use <a>, <b>, <i>, <strong>, <em>, <strike>, <code>.

Code blocks:
[code language="ruby/javascript/html/css/sass/bash"]
[/code]