When Ruby tries to execute an undefined method (or, in OOP terms, when an object is sent a message which it cannot handle), the error causes the program to exit.
When Ruby tries to execute an undefined method (or, in OOP terms, when an object is sent a message which it cannot handle), the error causes the program to exit.
The module_eval and class_eval methods operate on modules and classes rather than on objects.
There are some variations on the eval theme in the form of the methods named instance_eval, module_eval and class_eval.
The eval method can evaluate strings spanning many lines, making it possible to execute an entire program embedded in a string
There are times, however, when the results may not be what you are expecting. Look at the following, for instance:
exp = gets().chomp()
puts( eval( exp ))
puts( "#{exp}" )
So far we have modified classes and created new objects from existing classes. But how would you go about creating a completely new class at runtime?
adding and retrieving class variables from outside a class (using class_eval); it turns out that there are also methods de-signed to do this from inside a class.
With all these ways of modifying objects at your disposal, you may be concerned that objects are at risk of being modified unintentionally.