From: Massimiliano Mirra Date: 2002-07-04T21:10:41+09:00 Subject: Range thoughts (was: Re: (0..3) == (0...4) returning false?) On Thu, Jul 04, 2002 at 01:38:44AM +0900, Yukihiro Matsumoto wrote: > |I think you are right in your surprise. The confusion comes when one group > |sees a range of integers as a set of integers and another group sees a range > |as a continuum of floats. The "correct" answer depends of first determining > |the definition of a range: (1) a collection or (2) a single continuum. > > That's the point. My guess is a Range should work as both, depending > on a method called. I think I have to define useful / natural set of > method names for each definition of a range. And your guess is usually right. :-) Take this as just thinking out loud: FiniteSet < OrderedSet < DiscreteRange InfiniteSet < Continuum FiniteSet holds a finite number of elements, OrderedSet does the same but they are sorted, DiscreteRange does the same but all the items between the maximum and the minimum are present and at a fixed step from the previous one and the next one. InfiniteSet would hold an infinite number of elements (not really useful), Continuum holds all the elements between a minimum and a maximum. Another thought: Continuum < DiscreteRange DiscreteRange adds a condition to Continuum. While Continuum is all the numbers between a minimum and a maximum, DiscreteRange is all the numbers between a minimum and a maximum *such that* n % 1 == 0 (or n % 2 == 0 for the range to include only even numbers, and so on). DiscreteRange#include?(n) is true when n satisfies the condition. A block could be used for building the range (and an exception thrown when the return value of the block does not match the rule): min, max, step = 0, 10, 1 range = DiscreteRange.new(min, max) do |index| # index takes on 0,1,2... min + step*index end range.include?(5) # true range.include?(1.5) # false range.include?(11) # false It wouldn't be limited to uniformly stepped ranges. Acceleration against space, for example... mix, max, acceleration = 0, 100, 9.81 space_travelled = DiscreteRange.new(min, max) do |time| acceleration * time ** 2 end range.include?(39.24) # true range.include?(40) # false range.include?(245.25) # false Again, just thinking out loud... Massimiliano