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.
If you wanted it to build a product you’d find a way to get time to work on it. If you really wanted to start that new hobby you’d sacrifice something to find the time and money to do it.
I'll define a "Wannabe Entrepreneur" as someone who has never made money from their businesses. Here are the different types of wannabes.
In the past few years I've built go-carts, built a 200+ sq ft workshop, written several eBooks. How do I create a life where I have time to work on side projects?
Receive 5 Software projects mistakes we have made over the years and how to avoid them.