[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Open-ended ranges?

al_batuul

11/19/2007 12:45:00 PM

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Dear Clifford,

Defining an open-end class is a great idea; the rang from 1..nil or 0 to nil could help now, I suggest you add different types of parameters to the class such as time.

all the best,
al_batuul


Clifford Heath <no@spam.please.net> wrote:
Folk,

I found Sean Russell's 2001 posting on this subject, and had a play with creating
open ranges using of class instances - interesting to find you can even do this!

What I really want is to be able to define ranges like (1..nil), which is an
open-ended range starting from 1. Of course this doesn't work, but not for the
reason I expected. There seems to be some magic inside MRI that prevents it.

Like if I define:

class End
attr_reader :e
def initialize(e)
@e = e
end
def <=>(other)
@e <=> other.e
end
def succ
@e && @e.succ
end
end

Then I can create the following range: End.new(1) .. End.new(4)
but not this: End.new(1) .. End.new(nil)

Ok, perhaps that's fair enough. But inside class End, replace all
references to @e by @e.to_i (just for the experiment), and it works
(at least, it bitches that 0 is less than 1, but use
End.new(-1) .. End.new(nil)
and it's ok).

Now add puts "#{@e.to_i} <=> #{other.e}"; into the "<=>" method, and
Range "knows" that's not ok... when to my mind it should be ok if the
previous version was.

I haven't taken this any further yet. Until I know that some internal
magic isn't going to stop me making a Range class that works as advertised,
it didn't seem worth pursuing.

What do you think?

Clifford Heath.




---------------------------------
Never miss a thing. Make Yahoo your homepage.
4 Answers

Todd Benson

11/19/2007 12:54:00 PM

0

On Nov 19, 2007 6:44 AM, al_batuul <al_batuul@yahoo.com> wrote:
> Dear Clifford,
>
> Defining an open-end class is a great idea; the rang from 1..nil or 0 to nil could help now, I suggest you add different types of parameters to the class such as time.
>
> all the best,

Yes, would it help you really?

Let's make all data define itself. Let's make stuff work for now.
I'm not convinced that 1 to (what's supposed to be) some object of
NilClass will help anybody anywhere.

Todd

Yossef Mendelssohn

11/19/2007 3:14:00 PM

0

On Nov 19, 6:54 am, "Todd Benson" <caduce...@gmail.com> wrote:
> On Nov 19, 2007 6:44 AM, al_batuul <al_bat...@yahoo.com> wrote:
>
> > Dear Clifford,
>
> > Defining an open-end class is a great idea; the rang from 1..nil or 0 to nil could help now, I suggest you add different types of parameters to the class such as time.
>
> > all the best,
>
> Yes, would it help you really?
>
> Let's make all data define itself. Let's make stuff work for now.
> I'm not convinced that 1 to (what's supposed to be) some object of
> NilClass will help anybody anywhere.
>

This subject has come up more than once. I've worked on it myself, and
found a passable (if incomplete) solution.

What I don't understand is why so many people find an open-ended range
to be pointless. That is to say I do understand why --- because
they're set on iterating over the range --- but I don't understand why
they consider that to be the only use of a range.

Everyone I've seen who's interested in this (myself included) is using
these ranges for another reason, to indicate a range of acceptable (or
maybe even unacceptable values). A range like 5..Infinity is not
intended for iteration, but for comparison.

--
-yossef

Todd Benson

11/21/2007 8:55:00 AM

0

On Nov 19, 2007 9:13 AM, Yossef Mendelssohn <ymendel@pobox.com> wrote:
> On Nov 19, 6:54 am, "Todd Benson" <caduce...@gmail.com> wrote:

> What I don't understand is why so many people find an open-ended range
> to be pointless. That is to say I do understand why --- because
> they're set on iterating over the range --- but I don't understand why
> they consider that to be the only use of a range.

Well, I suppose for me it is sort of a weird use of the word Range.
Maybe OpenInterval would work better?

>
> Everyone I've seen who's interested in this (myself included) is using
> these ranges for another reason, to indicate a range of acceptable (or
> maybe even unacceptable values). A range like 5..Infinity is not
> intended for iteration, but for comparison.

5..Infinity is not 5..nil. I think it's also not a
5..some_number_to_be_determined_in_the_future. I guess that's the
point I was trying to make. It violates the generic least common
denominator practice present not only in the language, but also its
users.

>
> --
> -yossef
>
>
Sorry if I wasn't clear,
Todd

Todd Benson

11/21/2007 9:18:00 AM

0

On Nov 21, 2007 2:52 AM, Todd Benson <caduceass@gmail.com> wrote:
> On Nov 19, 2007 9:13 AM, Yossef Mendelssohn <ymendel@pobox.com> wrote:
> > On Nov 19, 6:54 am, "Todd Benson" <caduce...@gmail.com> wrote:
>
> > What I don't understand is why so many people find an open-ended range
> > to be pointless. That is to say I do understand why --- because
> > they're set on iterating over the range --- but I don't understand why
> > they consider that to be the only use of a range.
>
> Well, I suppose for me it is sort of a weird use of the word Range.
> Maybe OpenInterval would work better?
>
> >
> > Everyone I've seen who's interested in this (myself included) is using
> > these ranges for another reason, to indicate a range of acceptable (or
> > maybe even unacceptable values). A range like 5..Infinity is not
> > intended for iteration, but for comparison.
>
> 5..Infinity is not 5..nil. I think it's also not a
> 5..some_number_to_be_determined_in_the_future. I guess that's the
> point I was trying to make. It violates the generic least common
> denominator practice present not only in the language, but also its

OK, this last bit sounds a little harsh because I didn't clarify that
the least denominator thing is actually a benefit.

My first post sort of showed how you could use array indices as open
number ranges. Maybe, we could use a class word Interval instead of
OpenInterval or Range? I'll turn to whatever direction works for most
people, but I would like some semblance of a solid interval (i.e. left
and right sides).

Todd