Created
January 30, 2013 02:23
-
-
Save zspencer/4670044 to your computer and use it in GitHub Desktop.
blocks, procs, lambdas, and methods
This file contains hidden or 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
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