Created
November 27, 2014 17:50
-
-
Save tuxfight3r/5e60f3ad662bdfb0f8a3 to your computer and use it in GitHub Desktop.
ruby block proc and lambda example
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
# Block Examples | |
[1,2,3].each { |x| puts x*2 } # block is in between the curly braces | |
[1,2,3].each do |x| | |
puts x*2 # block is everything between the do and end | |
end | |
# Proc Examples | |
p = Proc.new { |x| puts x*2 } | |
[1,2,3].each(&p) # The '&' tells ruby to turn the proc into a block | |
proc = Proc.new { puts "Hello World" } | |
proc.call # The body of the Proc object gets executed when called | |
# Lambda Examples | |
lam = lambda { |x| puts x*2 } | |
[1,2,3].each(&lam) | |
lam = lambda { puts "Hello World" } | |
lam.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment