Ruby : Eval , MetaProgramming, Misc

Posted on March 6, 2008. Filed under: Uncategorized |

eval : evaluates a string as if it was code…

when create a class you open up a new scope …class Foo self is local variable in the scope end.

class_eval opens up the class..for adding methods to it…also this doesn’t start a new local scope….so all the global variables are available….basically what ever string/code is there executes in the local scope…it takes all the string code between do end block or a string….which gets evaluated….Also when the method gets added to the class “className” it will be available to within all class definition / between class end…This is equivalent to define_method on class KK ..end .the reciever is self here. so it is defined on the object of class..

instance_eval..code/string get executed on the object level or the scope of the object .to add new methods at the instance level .

just eval would just evaluate the code followed by the scope at which code needs to execute .This can be got by using the binding method / Binding object which contains the frozen variables….binding a simple statement on calling which the system documents the current variables and returns a Binding object which can be used with eval….

closure: is combination of binding + eval…….turned to into a object and can be called anytime….

send method in ruby is used to call the method on an object even though it is private…try using __send__ and the message to the whom and what parameters..

class << object usually open a new scope…..The previous variable are not available…

Make a Comment

Make A Comment: ( None so far )

blockquote and a tags work here.

  •  

    March 2008
    M T W T F S S
    « Feb   Apr »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • My Fav

Liked it here?
Why not try sites on the blogroll...