Created
May 13, 2014 12:37
-
-
Save zimbatm/cf324970a9b368da3c4b to your computer and use it in GitHub Desktop.
Useful little ruby extension
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
class Proc | |
def with_args(*args, &block) | |
proc do |obj| | |
call(obj, *args, &block) | |
end | |
end | |
end | |
class Symbol | |
def with_args(*args, &block) | |
proc do |obj| | |
obj.public_send(self, *args, &block) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
p [34, 55, 13].map(&:to_s.with_args(32)) | |
case "foo" | |
when :respond_to?.with_args(:to_s) # Because Proc implements === | |
puts "YES" | |
else | |
fail | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment