instance_eval and class_eval
The Module class represents a method named class_eval and the Object class defines a method named instance_eval, (module_eval is a synonym for class_eval.) Both of these methods evaluate Ruby code, like eval does, but there are two crucial differences. The first difference is that they evaluate the code in the context of the specified object or in the context of the specified module. Here are some examples:
o.instance_eval("@x") # It returns the value of o's instance variable @x
String.class_eval("def len; size; end") # define an instance method len of String to return #length of string
# another way
String.class_eval("alias len size") # The quoted code behaves just as if it was inside "class #String" and "end"
# Use instance_eval to define class method String.empty
String.instance_eval("def empty; ''; end")

You can also try this code with Online Ruby Compiler
Run Code
The crucial difference between instance_eval and class_eval when the code being evaluated contains a method definition. instance_eval defines singleton methods of the object (and this results in class methods when it is called on a class object). class_eval defines regular instance methods. The second significant difference between these two methods and the global eval is that instance_eval and class_eval can accept a block of code to evaluate. When passed a block instead of a string, the code in the block is executed in the appropriate context. Here, therefore, are alternatives to the previously shown invocations:
o.instance_eval { @x }
String.class_eval {
def len
size
end
}
String.class_eval { alias len size }
String.instance_eval { def empty; ""; end }

You can also try this code with Online Ruby Compiler
Run Code
“instance_exec” and “class_exec”
Ruby 1.9 defines 2 more evaluation methods: class_exec and instance_exec. These methods consider a block (but not a string) of code in the context of the receiver object, as class_eval and instance_eval do. The difference is that the exec methods receive arguments and pass them to the block. Thus, the code block is evaluated in the context of the specified object, with parameters whose values come from outside the object.
Frequently Asked Questions
What is Ruby?
Ruby is a powerful and simple object-oriented programming language, created by Yukihiro Matsumoto. Ruby is good at text processing. All the things in Ruby is an objects, and Ruby has blocks, iterators, and meta-classes.
You can use Ruby to experiment with prototypes, write servers, and for everyday programming tasks. As a fully-integrated object-oriented language, it scales quite well.
What are some of the features offered by Ruby?
- Basic OO features ( methods, classes, objects, and so on),
- Simple syntax,
- Special OO features (mixins, singleton methods, renaming, and so on),
- Operator overloading,
- Exception handling,
- Iterators and closures,
- Garbage collection
What does * prepend to an argument mean in Ruby?
When used as part of a formal parameter list, the * allows arbitrary numbers of arguments to be passed to a method by collecting them into an array and assigning that array to the starred parameter.
What is a singleton method in Ruby?
It is an instance method associated with one specific object.
How can you change the visibility of a method in Ruby?
We change the visibility of methods using public, private, and protected. When utilized with parameters, they change the visibility of the named methods but when used without parameters during a class definition, they change the visibility of subsequent methods.
Conclusion
I hope this article helps you provide some meaningful insights on evaluation of Strings and Blocks in Ruby.
Check out Difference between Argument and Parameter to grow your knowledge in this topic.
Check out this problem - Longest String Chain
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, Machine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!!