Skip to content

Instantly share code, notes, and snippets.

@duairc
Created September 11, 2009 11:01
Show Gist options
  • Save duairc/185228 to your computer and use it in GitHub Desktop.
Save duairc/185228 to your computer and use it in GitHub Desktop.
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