Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix Range#min, Range#max on X19 (issue #2041) #2042

Merged
merged 2 commits into from

2 participants

@kachick
Collaborator

I have tried the below points.

  • Fix #2041
  • Get more compatibility behaviors with MRI for Range#min, Range#max.
@brixen brixen merged commit 04c2c40 into from
@kachick kachick deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
23 kernel/common/range19.rb
@@ -12,19 +12,28 @@ def first(n=undefined)
end
def max(&block)
- raise TypeError, "cannot exclude non Integer end value" if @end.kind_of?(Float) && @excl
+ return super(&block) if block_given? || (@excl && !@end.kind_of?(Numeric))
+ return nil unless @end > @begin
- return super(&block) if block_given?
- return nil if @end < @begin || (@excl && @end == @begin)
- return @end if @end.kind_of?(Float) || (!@end.kind_of?(Float) && !@excl)
- super
+ if @excl
+ unless @end.kind_of?(Integer)
+ raise TypeError, "cannot exclude non Integer end value"
+ end
+
+ unless @begin.kind_of?(Integer)
+ raise TypeError, "cannot exclude end value with non Integer begin value"
+ end
+
+ return @end - 1
+ end
+
+ @end
end
def min(&block)
return super(&block) if block_given?
return nil if @end < @begin || (@excl && @end == @begin)
- return @begin if @begin.kind_of?(Float)
- super
+ @begin
end
protected
View
12 spec/ruby/core/range/max_spec.rb
@@ -35,6 +35,18 @@
it "returns nil when the endpoint is less than the start point in a Float range" do
(3003.20..908.1111).max.should be_nil
end
+
+ it "returns end point when the range is Time..Time(included end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ (time_start..time_end).max.should equal(time_end)
+ end
+
+ it "raises TypeError when called on a Time...Time(excluded end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ lambda { (time_start...time_end).max }.should raise_error(TypeError)
+ end
end
end
View
12 spec/ruby/core/range/min_spec.rb
@@ -29,6 +29,18 @@
it "returns nil when the start point is greater than the endpoint in a Float range" do
(3003.20..908.1111).min.should be_nil
end
+
+ it "returns start point when the range is Time..Time(included end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ (time_start..time_end).min.should equal(time_start)
+ end
+
+ it "returns start point when the range is Time...Time(excluded end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ (time_start...time_end).min.should equal(time_start)
+ end
end
end
Something went wrong with that request. Please try again.