mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
c4bca7cbb3
Make "N for M" into the form "given N, expected M". As I worked, I noticed that the `argnum_error()` function had a part to include the method name in the message. I think this part is no longer needed by https://github.com/mruby/mruby/pull/5394. - Before this patch ```console % bin/mruby -e '[1, 2, 3].each 0' trace (most recent call last): [1] -e:1 -e:1:in each: 'each': wrong number of arguments (1 for 0) (ArgumentError) ``` - After this patch ```console % bin/mruby -e '[1, 2, 3].each 0' trace (most recent call last): [1] -e:1 -e:1:in each: wrong number of arguments (given 1, expected 0) (ArgumentError) ```
51 lines
1017 B
Ruby
51 lines
1017 B
Ruby
class Proc
|
|
|
|
def ===(*args)
|
|
call(*args)
|
|
end
|
|
|
|
def yield(*args)
|
|
call(*args)
|
|
end
|
|
|
|
def to_proc
|
|
self
|
|
end
|
|
|
|
def curry(arity=self.arity)
|
|
type = :proc
|
|
abs = lambda {|a| a < 0 ? -a - 1 : a}
|
|
arity = abs[arity]
|
|
if lambda?
|
|
type = :lambda
|
|
self_arity = self.arity
|
|
if (self_arity >= 0 && arity != self_arity) ||
|
|
(self_arity < 0 && abs[self_arity] > arity)
|
|
raise ArgumentError, "wrong number of arguments (given #{arity}, expected #{abs[self_arity]})"
|
|
end
|
|
end
|
|
|
|
pproc = self
|
|
make_curry = proc do |given_args=[]|
|
|
__send__(type) do |*args|
|
|
new_args = given_args + args
|
|
if new_args.size >= arity
|
|
pproc[*new_args]
|
|
else
|
|
make_curry[new_args]
|
|
end
|
|
end
|
|
end
|
|
make_curry.call
|
|
end
|
|
|
|
def <<(other)
|
|
->(*args, **opts, &block) { call(other.call(*args, **opts, &block)) }
|
|
end
|
|
|
|
def >>(other)
|
|
->(*args, **opts, &block) { other.call(call(*args, **opts, &block)) }
|
|
end
|
|
|
|
end
|