create_function
-
Creating lambda style anonymous methods in Ruby is very convenient with the help of Ruby blocks. We create an anonymous method in Ruby by passing a block to the
Kernel#lamdamethod. This will result in aProcobject instance. While PHP’screate_functiontakes strings as arguments, the arguments and code for an anonymous function in Ruby are specified as block arguments, and the block body respectively.We can execute an anonymous method in Ruby using
Proc#callon our anonymous method.PHP
$add = create_function('$num1,$num2', 'return $num1 + $num2;'); print $add(2, 3); // => 5
Ruby
add = lambda do |num1, num2| num1 + num2 end p add.call(2, 3) # => 5
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

