String-indices

Hi
Interesting:
"a"[1] # nil
"a"[1,2] # "" # out of range
"a"[2,2] # nil # out of range

Not obvious to get nil or "" in the same cases!
Maybe that should be corrected...

Opti

It's explained in the documentation for String#:

"Element Reference --- If passed a single index, returns a substring
of one character at that index. If passed a start index and a
length, returns a substring containing length characters
starting at the start index. If passed a range, its beginning
and end are interpreted as offsets delimiting the substring to be returned."

"In these three cases, if an index is negative, it is counted from
the end of the string. For the start and range cases the starting index
is just before a character and an index matching the string's size.
Additionally, an empty string is returned when the starting index for
a character range is at the end of the string."

"Returns nil if the initial index falls outside the string or the
length is negative."

So, a[1,2] is not out of range, it's at the end of the string.

Regards,
Marcus

···

Am 18.11.2016 um 15:08 schrieb Die Optimisten:

Interesting:
"a"[1] # nil
"a"[1,2] # "" # out of range
"a"[2,2] # nil # out of range

Not obvious to get nil or "" in the same cases!
Maybe that should be corrected...

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

One could argue though that these two should return the same because
they can be viewed as equivalent:

irb(main):001:0> "a"[1]
=> nil
irb(main):002:0> "a"[1,1]
=> ""

The subtlety here is that the call with two arguments really means
"return the string starting at position (arg 1) and with max length
(arg 2) truncated at the end of the source string". In contrast the
first form means "return the one character string at the given
position or nil if out of range".

Kind regards

robert

···

On Fri, Nov 18, 2016 at 3:40 PM, <sto.mar@web.de> wrote:

Am 18.11.2016 um 15:08 schrieb Die Optimisten:

Interesting:
"a"[1] # nil
"a"[1,2] # "" # out of range
"a"[2,2] # nil # out of range

Not obvious to get nil or "" in the same cases!
Maybe that should be corrected...

It's explained in the documentation for String#:

"Element Reference --- If passed a single index, returns a substring
of one character at that index. If passed a start index and a
length, returns a substring containing length characters
starting at the start index. If passed a range, its beginning
and end are interpreted as offsets delimiting the substring to be returned."

"In these three cases, if an index is negative, it is counted from
the end of the string. For the start and range cases the starting index
is just before a character and an index matching the string's size.
Additionally, an empty string is returned when the starting index for
a character range is at the end of the string."

"Returns nil if the initial index falls outside the string or the
length is negative."

So, a[1,2] is not out of range, it's at the end of the string.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Interesting:
"a"[1] # nil
"a"[1,2] # "" # out of range
"a"[2,2] # nil # out of range

Not obvious to get nil or "" in the same cases!
Maybe that should be corrected...

It's explained in the documentation for String#:

For the start and range cases the starting index is just BEFORE a character and an index matching the string's size.

=> 0

Additionally, an empty string is returned when the starting index for
a character range is at the end of the string."

Maybe, I'm wrong, but "a"[0] == "a" ! (which is the end of the string, because start==end

"Returns nil if the initial index falls outside the string or the
length is negative."

So, a[1,2] is not out of range, it's at the end of the string.

Hi!
Why must this be that complicated?
I thought "a"[0] is "a", and a[1] is outside the string...

Besides it would be more comfortable if str[i,1] == str[i], which is not the case, if (and only if) i=length
(Why is "a"[1] in the range???)

Opti

···

On 2016-11-18 15:40, sto.mar@web.de wrote:

Am 18.11.2016 um 15:08 schrieb Die Optimisten:

Because you didn't read the documentation first. This isn't the first time that your first response is a copy/paste from the doco. I've done it myself. PLEASE start there. `ri String.` is quick and easy and accurate.

And because strings are complicated.

···

On Nov 18, 2016, at 09:52, Die Optimisten <inform@die-optimisten.net> wrote:

Why must this be that complicated?

Interesting:
"a"[1] # nil
"a"[1,2] # "" # out of range
"a"[2,2] # nil # out of range

Not obvious to get nil or "" in the same cases!
Maybe that should be corrected...

It's explained in the documentation for String#:

For the start and range cases the starting index is just BEFORE a
character and an index matching the string's size.

=> 0

Additionally, an empty string is returned when the starting index for
a character range is at the end of the string."

Maybe, I'm wrong, but "a"[0] == "a" ! (which is the end of the string,
because start==end

You're thinking close to the truth, but missing a subtlety:

When slicing a single char, it's as you imagine -- you get the
character at that position. When slicing a range, str[a,b] means
something more like "the substring from before-a to after-b or the end
of the string, whichever is sooner."

In this case, "before-a" means the cursor position between (a-1) and
a, and "after-b" means between b and (b+1).

"Returns nil if the initial index falls outside the string or the
length is negative."

So, a[1,2] is not out of range, it's at the end of the string.

Hi!
Why must this be that complicated?
I thought "a"[0] is "a", and a[1] is outside the string...

Think of assignment:

    a = ''
    a[0] #=> nil
    a[0,1] = 'A' # makes sense, a=='A'

    b = ''
    b[0,5] = 'x' # makes sense, b=='x'
    b[2,1] = 'z' # nonsense, 'x?z' ???

Besides it would be more comfortable if str[i,1] == str[i], which is not
the case, if (and only if) i=length
(Why is "a"[1] in the range???)

Comfortable for whom?

Cheers

···

On 19/11/2016, Die Optimisten <inform@die-optimisten.net> wrote:

On 2016-11-18 15:40, sto.mar@web.de wrote:

Am 18.11.2016 um 15:08 schrieb Die Optimisten:

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/