From: ara.t.howard@... Date: 2006-04-13T22:40:59+09:00 Subject: Re: Range#size On Thu, 13 Apr 2006, Meinrad Recheis wrote: > dear list, > > does anyone object to adding the method :size to class Range? > > class Range > def size > max-min+1 > end > end > > i think it's useful. maybe others too. i'd like to see it added to the core. > here is the test for Range#size in case it's really added. > > def test_range_size > assert_equal 1, (1..1).size > assert_equal 2, (1..2).size > assert_equal 3, (1..3).size > assert_equal 0, (1..1).size > assert_equal 1, (1...2).size > end > > what do you think? > -- henon this has been discussed before. the main issue is that ranges are not sequences, but merely endpoints. consider: (0.0 ... 1.0).size #=> Infinite, but how to compute? (0.0 ... 1).size #=> ? (Rational(5,6) .. Rational(5,7)).size #=> ? infinity = Float::MAX/Float::EPSILON (42.0 ... infinity).size #=> Infinite too a = Gem::Version.new '0.1.0' b = Gem::Version.new '0.42.0' (a .. b ).size #=> potentially Infinite (42 .. -1).size #=> this is a valid range if ranges are required to be continuous and bounded they become less useful. i think last time someone suggested an entirely new class... regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama