Created
September 11, 2009 11:01
-
-
Save duairc/185228 to your computer and use it in GitHub Desktop.
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 to_proc | |
if lambda? | |
takes = arity | |
flexible = takes < 0 | |
required = flexible ? ~takes : takes | |
proc do |*args, &block| | |
args = args.size == 1 && Array === args[0] ? args[0] : args | |
given = args.size | |
if not flexible and given > required | |
begin | |
self.(*args, &block) | |
rescue ArgumentError | |
args = args.take(args.length - 1) if given > required | |
retry | |
end | |
elsif given < required | |
args[required - 1] = nil | |
self.(*args, &block) | |
else | |
self.(*args, &block) | |
end | |
end.tap do |p| | |
p.define_singleton_method(:arity){takes} | |
end | |
else | |
self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment