"TypeError: no implicit conversion of Symbol into Integer" that is raised when I run `"abc"[:symbol]` is a bit confusing

Hi,
When I debug Ruby code, it's confusing for me to debug the error that appears in Case 2 below becauseI think the "no implicit conversion of Symbol into Integer" is not intuitive in this situation.
I want to know how this behavior was implemented. Could you tell me the reason or the issue related to this behavior?I could not find it as far as I searched on https://bugs.ruby-lang.org/issues.
```# Case 1'abc'[0]
# => "a"
# Case 2'abc'[:something]
# TypeError: no implicit conversion of Symbol into Integer
# Case 3'abc'['something']
# => nil```
Thanks,
Shinohara

Hi Shinohara,

This is because the method `[]` on the String class does not define
behavior when a symbol is passed.
Use of "string"[x] is processed by a method called `String[](arg)`. It can
take an int, a range, or a string.

https://ruby-doc.org/core-2.6/String.html#method-i-5B-5D

I think the authors of Ruby's core classes didn't think converting the
symbol to a string made sense.
I tend to agree.

Hope this helps.

Andy

<https://ruby-doc.org/core-2.6/String.html#method-i-5B-5D>

ยทยทยท

On Thu, Jan 30, 2020 at 10:24 AM Shinohara Teruki <ts_3156@yahoo.co.jp> wrote:

Hi,

When I debug Ruby code, it's confusing for me to debug the error that
appears in Case 2 below because
I think the "no implicit conversion of Symbol into Integer" is not
intuitive in this situation.

I want to know how this behavior was implemented. Could you tell me the
reason or the issue related to this behavior?
I could not find it as far as I searched on
https://bugs.ruby-lang.org/issues.

# Case 1
'abc'[0]
# => "a"

# Case 2
'abc'[:something]
# TypeError: no implicit conversion of Symbol into Integer

# Case 3
'abc'['something']
# => nil

Thanks,

Shinohara

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
---
Cofounder - https://nimbleindustries.io
Resume - https://andylibby.org/
alibby@andylibby.org