> http://talklikeaduck.denhaven2.com/articles/2007/09/20/duck-a-la-range
Well, the fact that the documentation says "must respond to" and "as
long as" disallows in my view to pass objects that do not respond to
#<=> and #succ. I think that is clear and unrelated to duck typing.
Of course that may indicate that the documentation needs a different
wording, but the current docs are clear and according to them if you
pass an object that does not respond to #succ to the Range
constructor the object is invalid, the code is invalid, albeit it may
run.
And as we know all documentation is perfect.
There are at least two uses of Ranges in ruby.
1) To represent a range for the purposes of determining if something
falls within that range.
(98.0..99.1).include?(temperature) is a valid example of such a use.
2) As an collection of elements. This case does require succ and <=>
to be able to enumerate
the elements.
Logical ranges, which have a logical expression as the start and
finish aren't really range objects but they use the notation.
The first usage does not require the start and end elements to support
succ, this is a fault of the documentation.
Note that the documentation OMITS explicitly mentioning that start
must be less than end, for the range to be non-empty. it only gives
examples to imply that.
About rationals: I claim that the definition of a closed Range in
Ruby (theoretically) allows for infinite ranges. That's my point. I
prove that in ruby-talk giving an example.
An example which conveniently implements the succ and <=> methods as ...
Hand waving is not an example which gives a proof.
Now, the example uses the rationals because what I want to show
follows easily from the fact that Q is bijectable with N, which is a
basic result in set theory. (See proof in the mailing list.)
Density is always relative to an ordering, and for brevity what I do
is to change the order using a standard technique which consists of
defining stuff into one set by transferring it from another through a
bijection. The fact that the ordenary order of Q makes Q dense is not
relevant to this discussion at all. You see a class with #<=> and
#succ that provides an infinite Range.
And ordering is what ranges are all about. Since your handwaving
example is a bit hard to follow, I'm not sure how you handle having a
range between two arbitrary rationals and ensure that the start is
less than the end in general.
But if you really want a Range of rationals arranged so as to fit an
ordering picked so as to be able to generate a sequence but not in
natural sequence, I guess you can. Of course if you want the natural
sequence, I guess you are face with having to enumerate the entire
sequence so as to be able to sort it.
But that's just a way to support my claim, I could construct another
thing and show it gives infinite Ranges (again, theoretically). I am
sure I could simplify the proof for non-mathematicians taking N and
Inifinity or something close to that.
Being pragmatic, I'm much more interested in being able to use
(1.2..3.6).include?(a)
than usages which only seem useful in theory.
···
On 9/20/07, Xavier Noria <fxn@hashref.com> wrote:
On Sep 20, 2007, at 9:41 PM, Rick DeNatale wrote:
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/