Ruby : Block / Proc

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

Block/ Proc is piece of code in curly braces/nameless function which can be manipulated as an object.You can create an object of it by calling the lambda method of kernel and passing the code to it to get the proc object.Once you have the Proc object you call it by calling the method call and you can pass parameter to the code just like any other method.You can create a Proc object by calling Proc.new and passing the code block as parameter….Difference between Lambda method of create / Proc method….is

1. lambda: you cannot pass extra arguments / less argument to the block.but the ones that were created Proc.new you can pass more parameter..

A method can take a block (means it can take another function).This done by calling the yield in the method to call the nameless function and pass parameters to this .You can check if there is block passed during the method call by using the method block_given?…Another way to call this by direct call to the block as object call (Proc_Object.call).Inorder to say block.call you need to pass the block argument to the method….that argument will be prefixed with the & .And this always the last argument passed. and you can take the variable number of arguments using *args….

while passing the code block as parameter you can simply pass it a code block and it will be converted into a Proc Object by the & on the parameter of the function .If you make it as just a normal parameter/code block then you have to make it as Proc Object before calling …so & basically converted a block to a Proc Object.when you just say Proc.new….then it means it make the block that is coming as part of the function call to the Proc object….

Proc object from Proc.new is like code insert…so return would be the return for the entire method…but if you used Lambda call then it almost like a function call. so a return would just return to the method…

closure are nothing but code which attached to it scope variable .So when they are called back they resurrect the environment (variable that were part of it when it was frozen).But the problem in ruby is the SCOPE.for the does not create a scope where as do creates it….so within a do… end the variable defined creates a scope and if a closure was created then it would contain those details when resurrected….

Proc to Block -> &(proc) -> block…

symbol to to_proc…..open the class Symbol and create the to_proc method….def to_proc and create a lambda call with x and variable argument. and call the x.send(self,*args) self is the name of the symbol(upcase) x is the string or something from collection and *args is the variable arguments….if any passed from the called method when it is yields…..

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...