From 6af7ff8542489d2136509a7f4640c327f7040c51 Mon Sep 17 00:00:00 2001
From: Stuart Halloway
Date: Fri, 28 Mar 2008 06:09:34 -0400
Subject: [PATCH] Fixes for Range#step.
* previously failing specs pass
* new spec added to cover float/int difference
---
kernel/core/range.rb | 2 +-
spec/ruby/1.8/core/range/step_spec.rb | 11 ++++++++++-
spec/tags/ruby/1.8/core/range/step_tags.txt | 2 --
3 files changed, 11 insertions(+), 4 deletions(-)
delete mode 100644 spec/tags/ruby/1.8/core/range/step_tags.txt
diff --git a/kernel/core/range.rb b/kernel/core/range.rb
index 03a95be..c93956c 100644
--- a/kernel/core/range.rb
+++ b/kernel/core/range.rb
@@ -239,7 +239,7 @@ class Range
def step(step_size = 1, &block) # :yields: object
first, last = @begin, @end
- step_size = step_size.to_f #people might not pass numbers in. This stops them.
+ step_size = (Float === first) ? Float(step_size) : Integer(step_size)
raise ArgumentError, "step can't be negative" if step_size < 0
raise ArgumentError, "step can't be 0" if step_size == 0
diff --git a/spec/ruby/1.8/core/range/step_spec.rb b/spec/ruby/1.8/core/range/step_spec.rb
index ccb6715..db883bb 100644
--- a/spec/ruby/1.8/core/range/step_spec.rb
+++ b/spec/ruby/1.8/core/range/step_spec.rb
@@ -40,8 +40,17 @@ describe "Range#step" do
obj = mock("mock")
lambda { (1..10).step(obj) }.should raise_error(TypeError)
end
+
+ # The behavior for floats and ints is consistent with MRI for the cases shown.
+ # Add specs as necessary if there are other cases.
+ it "coerces the argument to float if begin is a float" do
+ (obj = mock("0.1")).should_receive(:to_f).and_return(0.1)
+ res = []
+ (1.0..1.2).step(obj) {|x| res << x}
+ res.should == [1.0, 1.1]
+ end
- it "coerces the argument to intger by invoking to_int" do
+ it "coerces the argument to int if begin is anything other than a float" do
(obj = mock("2")).should_receive(:to_int).and_return(2)
res = []
(1..10).step(obj) {|x| res << x}
diff --git a/spec/tags/ruby/1.8/core/range/step_tags.txt b/spec/tags/ruby/1.8/core/range/step_tags.txt
deleted file mode 100644
index 1dd76a9..0000000
--- a/spec/tags/ruby/1.8/core/range/step_tags.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-fails:Range#step raises TypeError if the argument is non-numeric
-fails:Range#step coerces the argument to intger by invoking to_int
--
1.5.4.3