# class_eval vs. instance_eval in Ruby

*Published on January 15, 2023*

---


This post explains the difference between&nbsp;`class_eval`&nbsp;and&nbsp;`instance_eval`&nbsp;methods in Ruby. If you keep getting confused between them when reading or writing Ruby code, it should clarify things a little.

Consider the&nbsp;`Greeting`&nbsp;class, which forms the basis of other examples.

```ruby
class Greeting
  def initialize(text)
    @text = text
  end
end
```

### class\_eval

`class_eval`&nbsp;evaluates the string or block in the context of the class, allowing you to reopen the class and define additional behavior on it.

Creating a method inside the block defines an instance method on the class. All&nbsp;`Greeting`&nbsp;instances can call&nbsp;`greet`.

```ruby
Greeting.class_eval do
  def greet
    puts  "#{self.object_id}: #{@text}"
  end
end

greeting_one = Greeting.new "Hello World"
greeting_one.greet # 80: Hello World

greeting_two = Greeting.new "How are you?"
greeting_two.greet # 100: How are you?
```

This is the same as explicitly defining the&nbsp;`greet`&nbsp;method in the&nbsp;`Greeting`&nbsp;class. The above code is equivalent to:

```ruby
class Greeting
  def greet
    puts "#{self.object_id}: #{@text}"
  end
end
```

**Benefits**

1. The first benefit of&nbsp;`class_eval`&nbsp;is that you can call it on a variable pointing to a class, allowing you to add behavior to a class dynamically.
2. When using&nbsp;`class_eval`, the code will fail immediately, if the class doesn’t exist (or if you misspelled the class name). So you can’t&nbsp;_**accidentally**_&nbsp;add code to a class that doesn’t exist.

The&nbsp;`class_eval`&nbsp;method is only available on classes (modules, to be precise). You can’t call it on instances, i.e.&nbsp;`instance_name.class_eval`&nbsp;doesn’t exist. Ruby throws the error&nbsp;`undefined_method class_eval`.

```ruby
greeting_one = Greeting.new "Hello World"

# undefined method 'class_eval'
greeting_one.class_eval do
  puts "Error: Method doesn't exist"
end
```

### instance\_eval

`instance_eval`&nbsp;evaluates the code in the context of the receiving object. When you call it on a class, e.g.&nbsp;`ClassName.instance_eval`, the receiving object is the class, which is an instance of&nbsp;`Class`. For example,&nbsp;`Greeting`&nbsp;is an instance of&nbsp;`Class`.

```javascript
irb(main):043:0> Greeting.class
=> Class
```

If you create a method inside the block, it defines a class method. It’s associated with the class object but not visible to instances of that class.

```ruby
Greeting.instance_eval do
  def welcome
    puts "#{self.object_id}: This is greeting"
  end
end

greeting_one = Greeting.new "Hello World"
greeting_one.welcome # Error: undefined method 'welcome'

Greeting.welcome # 120: This is a greeting
```

> When the code is executing, the variable&nbsp;`self`&nbsp;is set to the receiving object to set the context, giving the code access to the object’s instance variables and private methods.

Similarly, calling&nbsp;`instance_eval`&nbsp;on an instance, e.g.&nbsp;`instance_name.instance_eval`&nbsp;defines a method on that specific instance. No other instances of the class can access that method.

```ruby
greeting_one.instance_eval do
  def personal_greeting
    puts "Specific to greeting one"
  end
end

# Specific to greeting one
greeting_one.personal_greeting

greeting_two = Greeting.new "Hello"

# undefined method 'personal_greeting' for greeting_two
greeting_two.personal_greeting
```

* * *

### Summary

1. `class_eval`&nbsp;evaluates the string or block in the context of the class, allowing you to reopen the class and define additional behavior on it.
2. `instance_eval`&nbsp;evaluates the string or block in the context of the receiver object, allowing you to run code as if we were inside a method of this object, giving access to its private methods.

