-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Use String#+@ before mutating the result of Symbol#to_s #37303
Conversation
* String#+@ is available since Ruby 2.3. * See the upstream Ruby change making Symbol#to_s return a frozen String for efficiency: ruby/ruby#2437
FWIW, I also tried to use only immutable Strings, but the change is then larger: diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb
index c4e419f546..6d61f9a38d 100644
--- a/activesupport/lib/active_support/ordered_options.rb
+++ b/activesupport/lib/active_support/ordered_options.rb
@@ -40,13 +40,11 @@ def [](key)
def method_missing(name, *args)
name_string = name.to_s
- if name_string.chomp!("=")
- self[name_string] = args.first
+ if name_string.end_with?('=')
+ self[name_string[0..-2]] = args.first
else
- bangs = name_string.chomp!("!")
-
- if bangs
- self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
+ if name_string.end_with?("!")
+ self[name_string[0..-2]].presence || raise(KeyError.new("#{name.inspect} is blank"))
else
self[name_string]
end
|
Related ruby/ruby#2487. |
That's the only change I had to do when I tested ruby/ruby#2175 against Redmine. There's definitely some stuff that will break out there, but it's both relatively rare and extremely easy fixes. |
👍 to this. |
Thanks for the quick merge! |
Can we also backport it? Assuming this experiment isn't reverted (🤞) MRI 2.7 will be released in about 3 months, so It would be better if people didn't hit that issue with the supported versions of Rails. |
Yeah, I will do that later today (l’m out of home now). |
Could we backport this change to both Rails 6.0 and 5.2 so people can use Ruby 2.7 on those Rails versions? For 5.2 we can use |
Backported. |
Use String#+@ before mutating the result of Symbol#to_s
Use String#+@ before mutating the result of Symbol#to_s
Thank you! |
Use String#+@ before mutating the result of Symbol#to_s
for efficiency: Make Symbol#to_s return a frozen String, the same String for a given Symbol ruby/ruby#2437
Summary
Ruby 2.7 will return a frozen String for Symbol#to_s to avoid extra allocations:
ruby/ruby#2437
https://bugs.ruby-lang.org/issues/16150#note-18
That is currently an experimental change, but I think it would make a lot of sense and I would expect only few places need to be adapted to handle a frozen Symbol#to_s.