July 2011
Intermediate to advanced
400 pages
8h 46m
English
You can also use the module_eval and class_eval methods to retrieve the values of class variables (but bear in mind that the more you do this, the more your code becomes dependent on the implementation details of a class, thereby compromising encapsulation):
Y.class_eval( "@@x" )
In fact, class_eval can evaluate expressions of arbitrary complexity. You could, for example, use it to add new methods to a class by evaluating a string:
ob = X.new
X.class_eval( 'def hi;puts("hello");end' )
ob.hi #=> helloReturning to the earlier example of adding and retrieving class variables from outside a class (using class_eval), it turns out that there are also methods designed to do this from inside a class. The methods are called
Read now
Unlock full access