Skip to content

Instantly share code, notes, and snippets.

@benolee
Created October 13, 2011 20:11
Show Gist options
  • Save benolee/1285364 to your computer and use it in GitHub Desktop.
Save benolee/1285364 to your computer and use it in GitHub Desktop.
unary operator challenge
# 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