Bird
Raised Fist0
Rubyprogramming~10 mins

Class_eval and instance_eval in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a method to the class using class_eval.

Ruby
class MyClass
  class_eval do
    def greet
      puts [1]
    end
  end
end

obj = MyClass.new
obj.greet
Drag options to blanks, or click blank then click option'
AHello from class_eval!
B"Hello from instance_eval!"
C"Hello from class_eval!"
Dputs "Hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Using instance_eval instead of class_eval here.
2fill in blank
medium

Complete the code to define an instance variable using instance_eval.

Ruby
class MyClass
  def initialize
    @name = "Ruby"
  end
end

obj = MyClass.new
obj.instance_eval do
  @name = [1]
end

puts obj.instance_eval { @name }
Drag options to blanks, or click blank then click option'
AChanged by instance_eval
Bname
C:Changed
D"Changed by instance_eval"
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a string without quotes.
Trying to access @name without instance_eval.
3fill in blank
hard

Fix the error in the code by completing the blank to call instance_eval correctly.

Ruby
class MyClass
  def initialize
    @value = 10
  end
end

obj = MyClass.new
result = obj.[1] { @value * 2 }
puts result
Drag options to blanks, or click blank then click option'
Ainstance_eval
Bself_eval
Ceval
Dclass_eval
Attempts:
3 left
💡 Hint
Common Mistakes
Using class_eval instead of instance_eval.
Using eval which is a Kernel method, not related to the object.
4fill in blank
hard

Fill both blanks to add a class method using class_eval and call it.

Ruby
class MyClass
  [1].class_eval do
    def self.say_hello
      "Hello from class method"
    end
  end
end

puts MyClass.[2]
Drag options to blanks, or click blank then click option'
Aself
Binstance
Csay_hello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Calling class_eval on an instance.
Calling a method that does not exist.
5fill in blank
hard

Fill all three blanks to define an instance variable with instance_eval, define a method with class_eval, and call both.

Ruby
class MyClass
  [1].class_eval do
    def greet
      "Hello, #{@name}!"
    end
  end
end

obj = MyClass.new
obj.[2] do
  @name = [3]
end

puts obj.greet
Drag options to blanks, or click blank then click option'
Aself
Binstance_eval
C"Ruby Learner"
Dclass_eval
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up class_eval and instance_eval calls.
Not quoting the string for @name.

Practice

(1/5)
1. What is the main difference between class_eval and instance_eval in Ruby?
easy
A. class_eval and instance_eval both change the whole class.
B. instance_eval changes the class and affects all instances; class_eval changes only one object.
C. class_eval changes the class and affects all instances; instance_eval changes only one object.
D. class_eval and instance_eval both change only one object.

Solution

  1. Step 1: Understand class_eval behavior

    class_eval opens the class and changes it, so all instances see the change.
  2. Step 2: Understand instance_eval behavior

    instance_eval runs code in the context of a single object, changing only that object.
  3. Final Answer:

    class_eval changes the class and affects all instances; instance_eval changes only one object. -> Option C
  4. Quick Check:

    class_eval = class-wide, instance_eval = single object [OK]
Hint: class_eval affects all objects; instance_eval affects one [OK]
Common Mistakes:
  • Confusing which method affects the class vs. single object
  • Thinking both methods change the whole class
  • Assuming instance_eval changes all instances
2. Which of the following is the correct syntax to add a method greet to class Person using class_eval?
easy
A. Person.class_eval { def greet; 'Hi'; end }
B. Person.instance_eval { def greet; 'Hi'; end }
C. Person.class_eval def greet; 'Hi'; end
D. Person.instance_eval def greet; 'Hi'; end

Solution

  1. Step 1: Recall correct block syntax for class_eval

    class_eval takes a block with method definitions inside curly braces or do-end.
  2. Step 2: Check method definition placement

    Method greet must be defined inside the block passed to class_eval.
  3. Final Answer:

    Person.class_eval { def greet; 'Hi'; end } -> Option A
  4. Quick Check:

    class_eval uses block with method defs [OK]
Hint: Use curly braces or do-end block with class_eval [OK]
Common Mistakes:
  • Omitting braces or block for class_eval
  • Using instance_eval instead of class_eval for class methods
  • Writing method defs outside the block
3. What will be the output of this Ruby code?
class Dog
  def initialize(name)
    @name = name
  end
end

fido = Dog.new('Fido')
fido.instance_eval { def speak; "Woof! I'm #{@name}"; end }
puts fido.speak

rex = Dog.new('Rex')
puts rex.respond_to?(:speak)
medium
A. Woof! I'm Fido false
B. Woof! I'm Fido true
C. NoMethodError for fido.speak
D. Woof! I'm Fido NoMethodError for rex.speak

Solution

  1. Step 1: Understand instance_eval adds method to one object

    The speak method is added only to fido, not to other Dog instances.
  2. Step 2: Check outputs

    fido.speak prints "Woof! I'm Fido". rex.respond_to?(:speak) returns false because rex has no speak method.
  3. Final Answer:

    Woof! I'm Fido false -> Option A
  4. Quick Check:

    instance_eval adds method to one object only [OK]
Hint: instance_eval adds method to one object only [OK]
Common Mistakes:
  • Assuming all instances get the new method
  • Expecting true for rex.respond_to?(:speak)
  • Confusing instance_eval with class_eval
4. Identify the error in this code snippet:
class Cat
end

kitty = Cat.new
kitty.class_eval { def meow; 'Meow!'; end }
puts kitty.meow
medium
A. No error; outputs 'Meow!'
B. NoMethodError because class_eval is called on instance
C. SyntaxError due to missing block
D. RuntimeError because method already exists

Solution

  1. Step 1: Check receiver of class_eval

    class_eval is a method of Class, not of instances; calling it on an instance raises NoMethodError.
  2. Step 2: Verify execution

    Since kitty is an instance, kitty.class_eval raises NoMethodError; thus, puts kitty.meow is not reached.
  3. Final Answer:

    NoMethodError because class_eval is called on instance -> Option B
  4. Quick Check:

    class_eval is a Class method, not instance method [OK]
Hint: class_eval is called on classes, not instances [OK]
Common Mistakes:
  • Thinking class_eval can be called on instances
  • Expecting NoMethodError for instance.class_eval
  • Confusing class_eval usage with class-only restriction
5. Given this code, what will be the output?
class Car
  def initialize(model)
    @model = model
  end
end

car1 = Car.new('Toyota')
car2 = Car.new('Honda')

Car.class_eval do
  def info
    "Model: #{@model}"
  end
end

car1.instance_eval do
  def info
    "Special Model: #{@model.upcase}"
  end
end

puts car1.info
puts car2.info
hard
A. Special Model: TOYOTA Special Model: HONDA
B. Model: Toyota Model: Honda
C. Model: Toyota Special Model: HONDA
D. Special Model: TOYOTA Model: Honda

Solution

  1. Step 1: Understand class_eval adds method to all instances

    info method added by class_eval returns "Model: #{@model}" for all cars.
  2. Step 2: Understand instance_eval overrides method for one object

    car1.instance_eval redefines info only for car1, returning uppercase model with "Special Model:" prefix.
  3. Step 3: Check outputs

    car1.info prints "Special Model: TOYOTA"; car2.info prints "Model: Honda".
  4. Final Answer:

    Special Model: TOYOTA Model: Honda -> Option D
  5. Quick Check:

    class_eval affects all; instance_eval overrides one [OK]
Hint: instance_eval overrides method only on one object [OK]
Common Mistakes:
  • Assuming instance_eval changes all instances
  • Expecting both outputs to be 'Special Model'
  • Confusing which method is called on which object
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard