Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created October 11, 2014 17:23
Show Gist options
  • Save Mon-Ouie/e2f6036f4685cae70b64 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/e2f6036f4685cae70b64 to your computer and use it in GitHub Desktop.
def ap(*args, &block)
lambda do |recv, *new_args, &new_block|
recv.send(*(args + new_args), &(block || new_block))
end
end
class Object
def try_it(*arguments, &block)
puts "Called try_it on #{inspect} with #{arguments.inspect} and " +
"block: #{block.inspect}"
end
end
puts [{"foo" => "bar"}, {"foo" => "baz"}].map(&ap(:[], "foo"))
a = proc {}
b = proc {}
ap(:try_it, "foo").call 1
ap(:try_it, "foo").call 1, "bar"
ap(:try_it).call 1
ap(:try_it).call 1, "bar"
ap(:try_it, "foo", &a).call 1, "bar"
ap(:try_it, "foo").call(1, "bar", &b)
ap(:try_it, "foo", &a).call(1, "bar", &b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment