Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Created November 27, 2014 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxfight3r/5e60f3ad662bdfb0f8a3 to your computer and use it in GitHub Desktop.
Save tuxfight3r/5e60f3ad662bdfb0f8a3 to your computer and use it in GitHub Desktop.
ruby block proc and lambda example
# 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