Last active
August 29, 2015 14:21
-
-
Save prodis/0bc1752eeb611860cbfd to your computer and use it in GitHub Desktop.
Fernando's whiteboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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