Skip to content

NoMethodError error for String#[]= #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
razum2um opened this issue Aug 27, 2018 · 1 comment
Closed

NoMethodError error for String#[]= #26

razum2um opened this issue Aug 27, 2018 · 1 comment

Comments

@razum2um
Copy link

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

say = "I love Ruby"
say['love'] = "*love*"
say

expected

I *love* Ruby

actual

NoMethodError: undefined method `[]=' for "I love Ruby"

this chunk of code is notably shown on ruby homepage

@razum2um razum2um changed the title Syntax error for String#[]= NoMethodError error for String#[]= Aug 27, 2018
@easydatawarehousing
Copy link
Collaborator

Thanks for reporting this issue. But this is something I don't think I can change. TryRuby uses Opal for running Ruby in a web browser. Opal transpiles ruby to javascript. In javascript strings are immutable. Opal copies this behavior, see undocumented features.

I am in the process of updating the Opal version used by TryRuby. Using the latest Opal (0.11.4) the string indexing does work.

say = "I love Ruby"
say['love']

it returns 'love' as expected.

Starting irb with immutable strings (RUBYOPT=--enable-frozen-string-literal irb) and using your example will produce a similar error on the []= method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants