Skip to content

Instantly share code, notes, and snippets.

@zspencer
Created January 30, 2013 02:23
Show Gist options
  • Save zspencer/4670044 to your computer and use it in GitHub Desktop.
Save zspencer/4670044 to your computer and use it in GitHub Desktop.
blocks, procs, lambdas, and methods
class String
def fuck_yea(&block)
each_char do |c|
p yield(c)
end
end
def fuck_yea_with_callback(callback)
each_char do |c|
callback.call(c)
end
end
def append_hahaha
self + "hahaha"
end
end
# This is a block
"word".fuck_yea { |c| c + "hahaha" }
# This is a function on instances
"bird".fuck_yea(&:append_hahaha)
# this is a proc
"word".fuck_yea_with_callback(Proc.new { |c, b, a| c + "hahaha" })
# this is a lambda, you'l notice it doesnt like having multiple arguments
"word".fuck_yea_with_callback(lambda { |c, b, a| c + "hahaha" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment