[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Change exclusive on Range

T. Onoma

10/11/2004 2:11:00 AM

Er...

How do I change the end exclusivity of a pre-existing range?

T.


7 Answers

Florian Gross

10/11/2004 2:32:00 AM

0

trans. (T. Onoma) wrote:

> How do I change the end exclusivity of a pre-existing range?

In theory this is impossible because Ranges are designed to be immutable
objects.

For other objects it is possible to change them via .send(:initialize),
but Ranges have a check for that.

I think the only option would be using evil-ruby, for now.

But why do you need this?

Regards,
Florian Gross

T. Onoma

10/11/2004 4:23:00 AM

0

On Sunday 10 October 2004 10:34 pm, Florian Gross wrote:
| trans. (T. Onoma) wrote:
| > How do I change the end exclusivity of a pre-existing range?
|
| In theory this is impossible because Ranges are designed to be immutable
| objects.
|
| For other objects it is possible to change them via .send(:initialize),
| but Ranges have a check for that.
|
| I think the only option would be using evil-ruby, for now.
|
| But why do you need this?

Testing some modifications to Range. One of them is the addition of
exclude_first? Then I wanted to try out this alternate notation:

-(0..9) # exclude end
+(0..9) # exclude first
~(0..9) # exclude both

Using unary operators, since they have no other meaning for ranges anyway.

Have a few other interesting changes, as well. It took a while, but it's
beginning to look quite nice --a good bit more flexible then the current
Range class.

T.

P.S. You may also notice why I'm concerned with precedence, too.



Jamis Buck

10/11/2004 4:29:00 AM

0

trans. (T. Onoma) wrote:
> On Sunday 10 October 2004 10:34 pm, Florian Gross wrote:
> | trans. (T. Onoma) wrote:
> | > How do I change the end exclusivity of a pre-existing range?
> |
> | In theory this is impossible because Ranges are designed to be immutable
> | objects.
> |
> | For other objects it is possible to change them via .send(:initialize),
> | but Ranges have a check for that.
> |
> | I think the only option would be using evil-ruby, for now.
> |
> | But why do you need this?
>
> Testing some modifications to Range. One of them is the addition of
> exclude_first? Then I wanted to try out this alternate notation:
>
> -(0..9) # exclude end
> +(0..9) # exclude first
> ~(0..9) # exclude both
>
> Using unary operators, since they have no other meaning for ranges anyway.

Interesting approach! That's rather novel. Thanks for sharing. :)

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


Gavin Sinclair

10/11/2004 5:01:00 AM

0

On Monday, October 11, 2004, 12:10:39 PM, trans. wrote:

> Er...

> How do I change the end exclusivity of a pre-existing range?

Perhaps you can't. Can you change anything else about a pre-existing
range? I'm intentionally not polluting my response by looking at the
docs. If I were to write a Range class from scratch, it would be
read-only.

So the answer to your question _would_ be: create a new range :)

Gavin




Mark Hubbart

10/11/2004 6:41:00 AM

0

On Mon, 11 Oct 2004 13:23:27 +0900, trans. (T. Onoma)
<transami@runbox.com> wrote:
> On Sunday 10 October 2004 10:34 pm, Florian Gross wrote:
>
>
> | trans. (T. Onoma) wrote:
> | > How do I change the end exclusivity of a pre-existing range?
> |
> | In theory this is impossible because Ranges are designed to be immutable
> | objects.
> |
> | For other objects it is possible to change them via .send(:initialize),
> | but Ranges have a check for that.
> |
> | I think the only option would be using evil-ruby, for now.
> |
> | But why do you need this?
>
> Testing some modifications to Range. One of them is the addition of
> exclude_first? Then I wanted to try out this alternate notation:
>
> -(0..9) # exclude end
> +(0..9) # exclude first
> ~(0..9) # exclude both
>
> Using unary operators, since they have no other meaning for ranges anyway.
>
> Have a few other interesting changes, as well. It took a while, but it's
> beginning to look quite nice --a good bit more flexible then the current
> Range class.
>
> T.
>
> P.S. You may also notice why I'm concerned with precedence, too.

Fascinating!! A bit esoteric, but pretty elegant nonetheless...

As for modifying the range, are you sure you want to? Maybe consider
just creating a new one. I suspect that someone might be surprised if
they do something like this:

rng = (0..5)
p -rng

... and later find that their range was modified in place. Less
surprising would be for it to return a new range with the
exclusive_end flag set to true.

cheers,
Mark


T. Onoma

10/11/2004 10:35:00 AM

0

On Monday 11 October 2004 01:00 am, Gavin Sinclair wrote:
| So the answer to your question _would_ be: create a new range :)

Doh! Of course, that is best idea. Mark said same thing.

Thanks guys!
T.


T. Onoma

10/11/2004 10:38:00 AM

0

On Monday 11 October 2004 02:40 am, Mark Hubbart wrote:
| Fascinating!! A bit esoteric, but pretty elegant nonetheless...
|
| As for modifying the range, are you sure you want to? Maybe consider
| just creating a new one. I suspect that someone might be surprised if
| they do something like this:
|
| rng = (0..5)
| p -rng
|
| ... and later find that their range was modified in place. Less
| surprising would be for it to return a new range with the
| exclusive_end flag set to true.

Just occurred to me: Does that mean range is Multiton, like Symbol ?

T.