Created
October 13, 2011 20:11
-
-
Save benolee/1285364 to your computer and use it in GitHub Desktop.
unary operator challenge
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
# In case you haven't seen +@ before, it works as a unary operator (as does -@) | |
# Example: | |
# | |
# a = A.new | |
# +a #=> RuntimeError: You must provide a block! | |
# a.send(:+@, &proc { "hello world!" }) #=> "hello world!" | |
class A | |
def +@ | |
if block_given? | |
yield | |
else | |
raise "You must provide a block!" | |
end | |
end | |
end | |
# Challenge: pass a block to method +@ without using :send | |
# Example: | |
# a = A.new | |
# +a do | |
# "hello world!" | |
# end #=> "hello world!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment