Skip to content

Instantly share code, notes, and snippets.

@prodis
Last active August 29, 2015 14:21
Fernando's whiteboard
Difference between blocks, procs and lambdas.
Blocks are not objects and can't be manipulates as objects. With procs and lambdas is possible to
create objects that represents a block.
Procs and lambdas have the 'call' method, that executes the block code associated in their creation.
A proc behaves like a block, different of lambdas that behaves like a method. For that reason,
lambdas need to receive the exact number of parameters defined in your declaration.
When you call 'return' in a proc, the return happens in the proc and in the method that called the proc.
When you call 'return' in a lambda, the return happens only for the lambda, keeping the regular
execution of the method that called the lambda.
# A method to reverse a string.
def reverse(text)
(text.size - 1).downto(0).inject('') do |new_text, i|
new_text << text[i]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment