OK. You got my point.
And your explanation seems logic to me.
Thanks.
Anyway, I still feel this is very strange...
"a"[-1..-2] #=> ""
""[-1..-2] #=> nil
My Conclusion is : For any no empty string, it exists exactly string's length + 1 of empty substring!
(location does matter)
Example: for "ab", there are exactly 3 empty substrings locate at "^a^b^" (^ shows empty string position)
s="ab"; s[-1..-2]="xxx"; p s #==> s = "axxxb"
s="ab"; s[-2..-3]="xxx"; p s #==> s = "xxxab"
s="ab"; s[1..0]="xxx"; p s #==> s = "axxxb", it is the same as s[-1..-2]
s="ab"; s[2..1]="xxx"; p s #==> s = "abxxx"
As you can see, there are exactly 3 empty substrings on "ab". (and you can re-assing)
cheer.
···
Hi, Ara!
I understand what you mean.
However, the return values should be consistent even if the range object
doesn't make sense.
> > s[-1..-2] #=> ""
> > s[-1..-3] #=> nil
How would you explain the inconsistency?
The only explanation I can think of is:
s[-1..-1] #=>"9"
s[-1..-2] #=>"", because one step back, one less character...
s[-1..-3] #=>nil, because there's no "less character" concept beyond empty
string.
I think getting a substring using range is somewhat confusing.
s[offset, length] is better understood.
Python employs a different way to slice a string which is similar to
str[from..to] in Ruby.
And I like the way it is explained.
s[from:to] #from and to are not the index of letters but that of "between
letters".
···
+---+---+---+---+---+
> H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
Sam
"D T" <email55555@hotmail.com> wrote in message
news:BAY22-F8HVZSOJl6byo00009438@hotmail.com...
OK. You got my point.
And your explanation seems logic to me.
Thanks.
Anyway, I still feel this is very strange...
"a"[-1..-2] #=> ""
""[-1..-2] #=> nil
My Conclusion is : For any no empty string, it exists exactly string's
length + 1 of empty substring!
(location does matter)
Example: for "ab", there are exactly 3 empty substrings locate at "^a^b^"
(^ shows empty string position)
s="ab"; s[-1..-2]="xxx"; p s #==> s = "axxxb"
s="ab"; s[-2..-3]="xxx"; p s #==> s = "xxxab"
s="ab"; s[1..0]="xxx"; p s #==> s = "axxxb", it is the same as
s[-1..-2]
s="ab"; s[2..1]="xxx"; p s #==> s = "abxxx"
As you can see, there are exactly 3 empty substrings on "ab". (and you can
re-assing)
cheer.
>Hi, Ara!
>
>I understand what you mean.
>However, the return values should be consistent even if the range object
>doesn't make sense.
>
> > > s[-1..-2] #=> ""
> > > s[-1..-3] #=> nil
>How would you explain the inconsistency?
>
>The only explanation I can think of is:
>
>s[-1..-1] #=>"9"
>s[-1..-2] #=>"", because one step back, one less character...
>s[-1..-3] #=>nil, because there's no "less character" concept beyond