[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String#[] behaviour

DNNX

12/18/2007 1:37:00 PM

'asd'[0...10] returns 'asd' while 'asd'[-10..-1] returns nil.

As far as I understand, such behaviour completely satisfies ruby
documentation (http://www.ruby-doc.org/core/classes/S...), but
it seems inconsistent to me.

Any thoughts?

Thanks
12 Answers

yermej

12/18/2007 3:07:00 PM

0

On Dec 18, 7:36 am, DNNX <6aLLIaPu...@gmail.com> wrote:
> 'asd'[0...10] returns 'asd' while 'asd'[-10..-1] returns nil.
>
> As far as I understand, such behaviour completely satisfies ruby
> documentation (http://www.ruby-doc.org/core/classes/S...), but
> it seems inconsistent to me.
>
> Any thoughts?
>
> Thanks

From the docs for String#[] at that link:

"Returns nil if the initial offset falls outside the string..."

Robert Klemme

12/18/2007 3:54:00 PM

0

2007/12/18, DNNX <6aLLIaPuMoB@gmail.com>:
> 'asd'[0...10] returns 'asd' while 'asd'[-10..-1] returns nil.
>
> As far as I understand, such behaviour completely satisfies ruby
> documentation (http://www.ruby-doc.org/core/classes/S...), but
> it seems inconsistent to me.
>
> Any thoughts?

On one hand you are right. On the other hand, begin and end indexes
are asymmetric anyway: you know that the starting index is always 0
but the ending index can have arbitrary values. I could not say I
came across this so far so for me personally this is a non issue. On
a larger scale it is probably a minor issue. Let's hear what others
say.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

yermej

12/18/2007 4:01:00 PM

0

On Dec 18, 9:06 am, yermej <yer...@gmail.com> wrote:
> On Dec 18, 7:36 am, DNNX <6aLLIaPu...@gmail.com> wrote:
>
> > 'asd'[0...10] returns 'asd' while 'asd'[-10..-1] returns nil.
>
> > As far as I understand, such behaviour completely satisfies ruby
> > documentation (http://www.ruby-doc.org/core/classes/S...), but
> > it seems inconsistent to me.
>
> > Any thoughts?
>
> > Thanks
>
> From the docs for String#[] at that link:
>
> "Returns nil if the initial offset falls outside the string..."

Nevermind what I said. Some days I don't read so good and stuff.

DNNX

12/18/2007 4:24:00 PM

0

On 18 ???, 17:54, Robert Klemme <shortcut...@googlemail.com> wrote:
> On one hand you are right. On the other hand, begin and end indexes
> are asymmetric anyway: you know that the starting index is always 0
> but the ending index can have arbitrary values. ...

Hm... On the other hand, end and begin indexes are asymmetric anyway:
you know that the ending index is always -1 but the starting index can
have arbitrary values.

Isn't this a symmetry?

Best regards,
Viktar

MonkeeSage

12/18/2007 11:42:00 PM

0

On Dec 18, 10:23 am, DNNX <6aLLIaPu...@gmail.com> wrote:
> On 18 ÄÅË, 17:54, Robert Klemme <shortcut...@googlemail.com> wrote:
>
> > On one hand you are right. On the other hand, begin and end indexes
> > are asymmetric anyway: you know that the starting index is always 0
> > but the ending index can have arbitrary values. ...
>
> Hm... On the other hand, end and begin indexes are asymmetric anyway:
> you know that the ending index is always -1 but the starting index can
> have arbitrary values.
>
> Isn't this a symmetry?
>
> Best regards,
> Viktar

No, because "-1" is a special value...it's got magic in it. It can
magically mean 5, or 10, or even 12 (because it's magic). ;) "-1" is
just sugar for #length, and #length is always a side-effect of a
container, whereas '0' is a constant entry point.

Regards,
Jordan

DNNX

12/19/2007 8:12:00 AM

0

On 19 ???, 01:42, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 18, 10:23 am, DNNX <6aLLIaPu...@gmail.com> wrote:
>
> > On 18 ÄÅË, 17:54, Robert Klemme <shortcut...@googlemail.com> wrote:
>
> > > On one hand you are right. On the other hand, begin and end indexes
> > > are asymmetric anyway: you know that the starting index is always 0
> > > but the ending index can have arbitrary values.  ...
>
> > Hm... On the other hand, end and begin indexes are asymmetric anyway:
> > you know that the ending index is always -1 but the starting index can
> > have arbitrary values.
>
> > Isn't this a symmetry?
>
> > Best regards,
> > Viktar
>
> No, because "-1" is a special value...it's got magic in it. It can
> magically mean 5, or 10, or even 12 (because it's magic). ;) "-1" is
> just sugar for #length, and #length is always a side-effect of a
> container, whereas '0' is a constant entry point.
>
> Regards,
> Jordan

-1 is more special and magic than 0? Hm... 0 also can magically mean
-6, -11, or even -13 (because it's magic too).

-1 is sugar for #length? Not sure I understand correctly. Never heard
such an interpretation of -1 earlier. Why #length but not #length-1?
Why 0 is not
sugar for -#length? What do you mean saying -1 is a sugar for
something?

0 is a constant entry point? Great, -1 is a constant exit point.

Anyway, is there any symmetry or no, I still believe that returning
'asd' in one case and nil in other is not consistent (please see my
example in the first message).

Regards,
Viktar

Michal Suchanek

12/19/2007 10:20:00 AM

0

On 19/12/2007, MonkeeSage <MonkeeSage@gmail.com> wrote:

> No, because "-1" is a special value...it's got magic in it. It can
> magically mean 5, or 10, or even 12 (because it's magic). ;) "-1" is
> just sugar for #length, and #length is always a side-effect of a
> container, whereas '0' is a constant entry point.

-1 is as constant as 0. And because of its magic when used as
container index it always means the end. And really length - 1, not
just length. And at that place there is always the last object unless
the container is empty. The same way as the first object is at 0.

Thanks

Michal

Pasha Nigerish

12/19/2007 11:50:00 AM

0

Michal Suchanek wrote:
> On 18/12/2007, DNNX <6aLLIaPuMoB@gmail.com> wrote:
>>
> The asymmetry is in that you can chop off "at most 10 characters from
> the start" with 0...10 but not "at most 10 characters from the end"
> with -10..-1 because the start that has to be inside the string is the
> one of which you cannot be sure. You cannot swap the bounds because
> you get an empty string then.
>
> So the symmetric rule for range indexing would be something like this:
> ...skipped...

So we must become clear with range indexing: I think it's perfectly
legal to return intersection of an array/string with range instead of
nil in a case of negative start.
This can be done via one-line patch in range.c:615 (as in trunk) - just
assume `beg = 0` instead of `goto out_of_range`
Thus we'll have at least more perl-compatible behavior =) i.e. just as
'abc'[0..6] is 'abc' now, so 'abc'[-6..-1] will be 'abc' as well.

One problem I see in this assumption: 'abc'[4..6] and 'abc'[-6..-4] will
return '' instead of nil.


--
Posted via http://www.ruby-....

Michal Suchanek

12/19/2007 12:01:00 PM

0

On 19/12/2007, Pasha Nigerish <reedsol@tut.by> wrote:
> Michal Suchanek wrote:
> > On 18/12/2007, DNNX <6aLLIaPuMoB@gmail.com> wrote:
> >>
> > The asymmetry is in that you can chop off "at most 10 characters from
> > the start" with 0...10 but not "at most 10 characters from the end"
> > with -10..-1 because the start that has to be inside the string is the
> > one of which you cannot be sure. You cannot swap the bounds because
> > you get an empty string then.
> >
> > So the symmetric rule for range indexing would be something like this:
> > ...skipped...
>
> So we must become clear with range indexing: I think it's perfectly
> legal to return intersection of an array/string with range instead of
> nil in a case of negative start.
> This can be done via one-line patch in range.c:615 (as in trunk) - just
> assume `beg = 0` instead of `goto out_of_range`
> Thus we'll have at least more perl-compatible behavior =) i.e. just as
> 'abc'[0..6] is 'abc' now, so 'abc'[-6..-1] will be 'abc' as well.
>
> One problem I see in this assumption: 'abc'[4..6] and 'abc'[-6..-4] will
> return '' instead of nil.

You can still test the lower bound is inside the string. It's just
that with negative ranges the lower bound is the second number, not
the first.

Thanks

Michal

MonkeeSage

12/19/2007 2:39:00 PM

0

On Dec 19, 2:12 am, DNNX <6aLLIaPu...@gmail.com> wrote:
> On 19 ???, 01:42, MonkeeSage <MonkeeS...@gmail.com> wrote:
>
>
>
> > On Dec 18, 10:23 am, DNNX <6aLLIaPu...@gmail.com> wrote:
>
> > > On 18 ÄÅË, 17:54, Robert Klemme <shortcut...@googlemail..com> wrote:
>
> > > > On one hand you are right. On the other hand, begin and end indexes
> > > > are asymmetric anyway: you know that the starting index is always 0
> > > > but the ending index can have arbitrary values. ...
>
> > > Hm... On the other hand, end and begin indexes are asymmetric anyway:
> > > you know that the ending index is always -1 but the starting index can
> > > have arbitrary values.
>
> > > Isn't this a symmetry?
>
> > > Best regards,
> > > Viktar
>
> > No, because "-1" is a special value...it's got magic in it. It can
> > magically mean 5, or 10, or even 12 (because it's magic). ;) "-1" is
> > just sugar for #length, and #length is always a side-effect of a
> > container, whereas '0' is a constant entry point.
>
> > Regards,
> > Jordan
>
> -1 is more special and magic than 0? Hm... 0 also can magically mean
> -6, -11, or even -13 (because it's magic too).
>
> -1 is sugar for #length? Not sure I understand correctly. Never heard
> such an interpretation of -1 earlier. Why #length but not #length-1?
> Why 0 is not
> sugar for -#length? What do you mean saying -1 is a sugar for
> something?
>
> 0 is a constant entry point? Great, -1 is a constant exit point.
>
> Anyway, is there any symmetry or no, I still believe that returning
> 'asd' in one case and nil in other is not consistent (please see my
> example in the first message).
>
> Regards,
> Viktar

I think you (and Michal) missed my point. And yes, I should have said
#length-1. The point is, since there is *no such thing* as a negative
index -- 0 is the *first* index -- and "-1" (or -anynumber) is just
sugar (i.e., just a more convenient syntax for writing #length-
whatever), what you're asking is for ranges such as [-7..2] and [1..0]
to be meaningful. Taking your example, "'asd'[-10..-1]", this means
'asd'[-7..2] when you de-sugar it. Now in the other case,
"'asd'[0...10]", once you reach #length-1, you can stop and return
0..#length-1. But with 'asd'[-7..2], what are you supposed to do when
the start index is less than the first index (0)? Well, you could skip
ahead to the first index, sure, but it makes just as much sense (if
not more) to return nil/empty string. Same goes for cases such as
'asd'[-2..-3] (i.e., 'asd'[1..0]), where the start index is greater
than the end index.

Regards,
Jordan