Skip to content

Instantly share code, notes, and snippets.

@kronos
Created February 10, 2010 23:53
Show Gist options
  • Save kronos/301010 to your computer and use it in GitHub Desktop.
Save kronos/301010 to your computer and use it in GitHub Desktop.
From eedc242373f4d00ed281aec231443ccf076d3e84 Mon Sep 17 00:00:00 2001
From: Ivan Samsonov <hronya@gmail.com>
Date: Thu, 11 Feb 2010 02:46:11 +0300
Subject: [PATCH] Updates specs for some cases method#arity and proc#arity
---
spec/ruby/core/method/arity_spec.rb | 6 ++++++
spec/ruby/core/method/fixtures/classes.rb | 4 ++++
spec/ruby/core/proc/arity_spec.rb | 3 +++
3 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/spec/ruby/core/method/arity_spec.rb b/spec/ruby/core/method/arity_spec.rb
index 24f07ba..3880910 100644
--- a/spec/ruby/core/method/arity_spec.rb
+++ b/spec/ruby/core/method/arity_spec.rb
@@ -14,6 +14,12 @@ describe "Method#arity" do
@m.method(:two_req).arity.should == 2
end
+ it "returns n, where n is the number of required arguments and method created via defined_method" do
+ @m.method(:zero_defined_method).arity.should == 0
+ @m.method(:one_req_defined_method).arity.should == 1
+ @m.method(:two_req_defined_method).arity.should == 2
+ end
+
it "returns -(n+1), where n is the number of required arguments, when there is at least one optional argument" do
@m.method(:one_opt).arity.should == -1
@m.method(:one_req_one_opt).arity.should == -2
diff --git a/spec/ruby/core/method/fixtures/classes.rb b/spec/ruby/core/method/fixtures/classes.rb
index d7d0724..e74d6f7 100644
--- a/spec/ruby/core/method/fixtures/classes.rb
+++ b/spec/ruby/core/method/fixtures/classes.rb
@@ -68,6 +68,10 @@ module MethodSpecs
def one_req_one_opt_with_splat_and_block(a, b=nil, *c, &block); end
def two_req_one_opt_with_splat_and_block(a, b, c=nil, *d, &block); end
def one_req_two_opt_with_splat_and_block(a, b=nil, c=nil, *d, &block); end
+
+ class_eval { define_method(:zero_defined_method, Proc.new {||}) }
+ class_eval { define_method(:one_req_defined_method, Proc.new {|x|}) }
+ class_eval { define_method(:two_req_defined_method, Proc.new {|x, y|}) }
end
module MyMod
diff --git a/spec/ruby/core/proc/arity_spec.rb b/spec/ruby/core/proc/arity_spec.rb
index 1a175e4..797c4b2 100644
--- a/spec/ruby/core/proc/arity_spec.rb
+++ b/spec/ruby/core/proc/arity_spec.rb
@@ -26,18 +26,21 @@ describe "Proc#arity" do
end
it "if optional arguments, return the negative number of mandatory arguments using Proc.new " do
+ Proc.new { |*| }.arity.should == -1
Proc.new { |*a| }.arity.should == -1
Proc.new { |a, *b| }.arity.should == -2
Proc.new { |a, b, *c| }.arity.should == -3
end
it "if optional arguments, return the negative number of mandatory arguments using Kernel#lambda" do
+ lambda { |*| }.arity.should == -1
lambda { |*a| }.arity.should == -1
lambda { |a, *b| }.arity.should == -2
lambda { |a, b, *c| }.arity.should == -3
end
it "if optional arguments, return the negative number of mandatory arguments using Kernel#proc" do
+ proc { |*| }.arity.should == -1
proc { |*a| }.arity.should == -1
proc { |a, *b| }.arity.should == -2
proc { |a, b, *c| }.arity.should == -3
--
1.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment