[ruby-core:92941] [Ruby trunk Feature#15896] Symbol#+

From: merch-redmine@...
Date: 2019-06-03 17:34:04 UTC
List: ruby-core #92941
Issue #15896 has been updated by jeremyevans0 (Jeremy Evans).


I am against adding this to core.  Symbols represent identifiers, not data/text. Strings should be used for data/text. I don't think we should add methods to Symbol that further blur the line between String and Symbol.

I would rather see `:"#{s1}#{s2}"` for explicitly combining two symbols in the few cases that need to do so. That would be slower than the cached approach as it currently results in 3 string allocations, but I don't think it is likely to be a bottleneck.

One issue with the cached approach you used is that the cache doesn't use a weak map, so dynamic symbols that use `+` would never get garbage collected.

----------------------------------------
Feature #15896: Symbol#+
https://bugs.ruby-lang.org/issues/15896#change-78317

* Author: zeus (Zeus 81)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I've made this today
``` ruby
class Symbol
  @@cache_add = Hash.new {|h1,k1| h1[k1] = Hash.new {|h2,k2| h2[k2] = :"#{k1}#{k2}"}}
  def +(s) @@cache_add[self][s] end
end

:abc + :def
# => :abcdef
```
I thought it was so awesome it should become standard.



-- 
https://bugs.ruby-lang.org/

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

In This Thread

Prev Next