Skip to content

Instantly share code, notes, and snippets.

@joekunin
Created March 15, 2015 19:29
Show Gist options
  • Save joekunin/af37a2a2323ba603a004 to your computer and use it in GitHub Desktop.
Save joekunin/af37a2a2323ba603a004 to your computer and use it in GitHub Desktop.
# https://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
# 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