From: Alexander Date: 2005-11-25T11:51:30+09:00 Subject: Re: Improving min/max for Range On 11/25/05, Yukihiro Matsumoto wrote: > |Good point. I think max should throw if the number doesn't support the > |integer protocol; there's simply no way to handle it intelligently. > |For int-type types, I suggest that (end.to_int - 1) should be used, > |since there is no #pred counterpart to #succ. > > I've just committed my implementation of Range#min and #max to CVS > HEAD. Check if you have interest. > > matz. I see you decided to return nil instead of throwing; acceptable, I suppose. More sneaky is how you let Enumerable handle the case when the end is excluded -- I would have preferred to do that only do if #end was not an integer. Here's a unit test patch, btw: diff -u -r1.5 test_range.rb --- test/ruby/test_range.rb 5 Aug 2005 23:56:02 -0000 1.5 +++ test/ruby/test_range.rb 25 Nov 2005 02:47:06 -0000 @@ -31,4 +31,24 @@ def test_duckrange assert_equal("bc", "abcd"[DuckRange.new(1,2)]) end + + def test_min + assert_equal(1, (1..2).min) + assert_equal(nil, (2..1).min) + assert_equal(1, (1...2).min) + + assert_equal(1.0, (1.0..2.0).min) + assert_equal(nil, (2.0..1.0).min) + assert_equal(1, (1.0...2.0).min) + end + + def test_max + assert_equal(2, (1..2).max) + assert_equal(nil, (2..1).max) + assert_equal(1, (1...2).max) + + assert_equal(2.0, (1.0..2.0).max) + assert_equal(nil, (2.0..1.0).max) + assert_raise(TypeError) { (1.0...2.0).max } + end end Alexander.