Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rubinius/rubinius
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4b2a14b39eda
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c545bda29963
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on May 3, 2016

  1. Copy the full SHA
    ebc319c View commit details
  2. Fix Proc#arity.

    rideliner committed May 3, 2016
    Copy the full SHA
    835bc0a View commit details

Commits on May 4, 2016

  1. Merge pull request #3649 from rideliner/arity

    Fix Proc#arity.
    brixen committed May 4, 2016
    Copy the full SHA
    c545bda View commit details
Showing with 238 additions and 1 deletion.
  1. +6 −1 core/proc.rb
  2. +232 −0 spec/ruby/core/proc/arity_spec.rb
7 changes: 6 additions & 1 deletion core/proc.rb
Original file line number Diff line number Diff line change
@@ -96,7 +96,12 @@ def arity
return arity < 0 ? -1 : arity
end

@block.arity
arity = @block.arity
return arity if self.lambda? ||
@block.compiled_code.splat ||
@block.compiled_code.total_args == arity

-arity - 1
end

alias_method :===, :call
232 changes: 232 additions & 0 deletions spec/ruby/core/proc/arity_spec.rb
Original file line number Diff line number Diff line change
@@ -423,4 +423,236 @@
end
end
end

context "for instances created with proc { || }" do
context "returns zero" do
evaluate <<-ruby do
@a = proc { }
@b = proc { || }
ruby

@a.arity.should == 0
@b.arity.should == 0
end

evaluate <<-ruby do
@a = proc { |&b| }
ruby

@a.arity.should == 0
end

evaluate <<-ruby do
@a = proc { |a=1| }
@b = proc { |a=1, b=2| }
ruby

@a.arity.should == 0
@b.arity.should == 0
end

evaluate <<-ruby do
@a = proc { |a: 1| }
@b = proc { |a: 1, b: 2| }
ruby

@a.arity.should == 0
@b.arity.should == 0
end

evaluate <<-ruby do
@a = proc { |**k, &l| }
@b = proc { |a: 1, b: 2, **k| }
ruby

@a.arity.should == 0
@b.arity.should == 0
end

evaluate <<-ruby do
@a = proc { |a=1, b: 2| }
@b = proc { |a=1, b: 2| }
ruby

@a.arity.should == 0
@b.arity.should == 0
end
end

context "returns positive values" do
evaluate <<-ruby do
@a = proc { |a| }
@b = proc { |a, b| }
@c = proc { |a, b, c| }
@d = proc { |a, b, c, d| }
ruby

@a.arity.should == 1
@b.arity.should == 2
@c.arity.should == 3
@d.arity.should == 4
end

evaluate <<-ruby do
@a = proc { |a, b=1| }
@b = proc { |a, b, c=1, d=2| }
ruby

@a.arity.should == 1
@b.arity.should == 2
end

evaluate <<-ruby do
@a = lambda { |a:| }
@b = lambda { |a:, b:| }
@c = lambda { |a: 1, b:, c:, d: 2| }
ruby

@a.arity.should == 1
@b.arity.should == 1
@c.arity.should == 1
end

evaluate <<-ruby do
@a = proc { |a, b:| }
@b = proc { |a, b:, &l| }
ruby

@a.arity.should == 2
@b.arity.should == 2
end

evaluate <<-ruby do
@a = proc { |a, b, c:, d: 1| }
@b = proc { |a, b, c:, d: 1, **k, &l| }
ruby

@a.arity.should == 3
@b.arity.should == 3
end

evaluate <<-ruby do
@a = proc { |a, b, c:, d: 1| }
@b = proc { |a, b, c:, d: 1, **k, &l| }
ruby

@a.arity.should == 3
@b.arity.should == 3
end

evaluate <<-ruby do
@a = proc { |(a, (*b, c)), d=1| }
@b = proc { |a, (*b, c), d, (*e), (*), **k| }
ruby

@a.arity.should == 1
@b.arity.should == 5
end
end

context "returns negative values" do
evaluate <<-ruby do
@a = proc { |a=1, *b| }
@b = proc { |a=1, b=2, *c| }
ruby

@a.arity.should == -1
@b.arity.should == -1
end

evaluate <<-ruby do
@a = proc { |*| }
@b = proc { |*a| }
ruby

@a.arity.should == -1
@b.arity.should == -1
end

evaluate <<-ruby do
@a = proc { |a, *| }
@b = proc { |a, *b| }
@c = proc { |a, b, *c| }
@d = proc { |a, b, c, *d| }
ruby

@a.arity.should == -2
@b.arity.should == -2
@c.arity.should == -3
@d.arity.should == -4
end

evaluate <<-ruby do
@a = proc { |*a, b| }
@b = proc { |*a, b, c| }
@c = proc { |*a, b, c, d| }
ruby

@a.arity.should == -2
@b.arity.should == -3
@c.arity.should == -4
end

evaluate <<-ruby do
@a = proc { |a, *b, c| }
@b = proc { |a, b, *c, d, e| }
ruby

@a.arity.should == -3
@b.arity.should == -5
end

evaluate <<-ruby do
@a = proc { |a, b=1, c=2, *d, e, f| }
@b = proc { |a, b, c=1, *d, e, f, g| }
ruby

@a.arity.should == -4
@b.arity.should == -6
end

evaluate <<-ruby do
@a = proc { |*a, b: 1| }
@b = proc { |a=1, *b, c: 2, &l| }
ruby

@a.arity.should == -1
@b.arity.should == -1
end

evaluate <<-ruby do
@a = proc { |*a, **k| }
ruby

@a.arity.should == -1
end

evaluate <<-ruby do
@a = proc { |a=1, *b, c:, d: 2, **k, &l| }
ruby

@a.arity.should == -2
end

evaluate <<-ruby do
@a = proc { |a, (b, c), *, d:, e: 2, **| }
ruby

@a.arity.should == -4
end

evaluate <<-ruby do
@a = proc { |a, b=1, *c, d, e:, f: 2, **k, &l| }
@b = proc { |a, b=1, *c, d:, e:, f: 2, **k, &l| }
@c = proc { |a=0, b=1, *c, d, e:, f: 2, **k, &l| }
@d = proc { |a=0, b=1, *c, d:, e:, f: 2, **k, &l| }
ruby

@a.arity.should == -4
@b.arity.should == -3
@c.arity.should == -3
@d.arity.should == -2
end
end
end
end