ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "phluid61 (Matthew Kerwin)" <matthew@kerwin.net.au>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:51959] [ruby-trunk - Feature #7792] Make symbols and strings the same thing
Date: Thu, 7 Feb 2013 10:18:41 +0900	[thread overview]
Message-ID: <redmine.journal-35957.20130207101822@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-7792.20130206224824@ruby-lang.org


Issue #7792 has been updated by phluid61 (Matthew Kerwin).


rosenfeld (Rodrigo Rosenfeld Rosas) wrote:
> @drbrain, that means I'm unable to do things like
> 
> results << {id: id, name: name}
> 
> and have to use the old syntax all the time (and worrying about the differences all the time):
> 
> results << {'id' => id, 'name' => 'name'}

You say "old syntax," maybe you should think of it as the "general syntax."  Or, as I think of it, the "real syntax."  The new {id: id} syntax is special case sugar for Hashes keyed on Symbols, which we've already determined is not a great thing to be doing in most cases.

> I still believe MRI should try to optimize frozen strings instead of symbols and just make symbols behave like frozen strings. :symbol would be a shortcut to 'symbol'.freeze.
 
But symbols *aren't* strings. You can't do any of the string-specific things on them, except by first casting them to strings.  Why create a string, then use it to generate an (unrelated) type of object? Just create the symbol in the first place (as already happens).
 
> I don't really think any of those micro-benchmarks would justify all the hassle and unhappiness that symbols bring to Ruby programmers.
 
A simple solution presents itself: stop using them.  Or I should say, stop using them except when you absolutely must.

If we stop thinking of them as special/frozen/whatever strings, and stop using them as "strings with construction optimisation," this whole issue becomes irrelevant.  Then we can also stop using the symbol-specific {a: a} syntax, except when it really makes sense.  And voila! everything is gravy.

Earlier you also wrote:
> You can't know in advance if a hash returned by some external code is indexed by string or symbols. You have to test by yourself or check the documentation.

Indeed.  Similarly you can't know in advanced if it's keyed on integers, or CustomMagicClass instances.  Not a symbol-specific issue.  If the library authors cleared up their misconception that symbols are magic strings, you wouldn't have a problem.  Solution: work around their shortcomings, or find/write a better library.
----------------------------------------
Feature #7792: Make symbols and strings the same thing
https://bugs.ruby-lang.org/issues/7792#change-35957

Author: rosenfeld (Rodrigo Rosenfeld Rosas)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: Next Major


Recently I had to replace several of my symbols to plain strings in my project. Here is what happened:

I generated some results with some class that would add items to an array like this:

results << {id: 1, name: 'abc'}

Then I would store such results in cache using Redis, encoded as a JSON string. But then when I restore the data from cache the hash will be {'id' => 1, 'name' => 'abc'}.

This wasn't a problem until recently because I never used the results directly in the same request before and would always use the value stored on Redis and parsed by JSON.

But recently I had to use the values directly in a view. But then I had a problem because I would have to use symbols in the results for the first time and strings the next times when the result was available on cache. I really don't want to care about memory management in Ruby if possible and symbols forces me to abandon the new sexy hash syntax many times. Now I have to write

results << {'id' => 1, 'name' => 'abc}

when I'd prefer to write

results << {id: 1, name: 'abc}

This is not the first time I had a bad user experience due to symbols being different from strings. And I'm not the only one or ActiveSupport Hash#with_indifferent_access wouldn't be so popular and Rails wouldn't use it all the time internally.

It is really bad when you have to constantly check how you store your keys in your hashes. Am I using symbols or strings as keys? If you use the wrong type on plain hashes you can find a bad time debugging your code. Or you could just use Hash#with_indifferent_access everywhere, thus reducing performance (I guess) and it is pretty inconvenient anyway.

Or if you're comparing the keys of your hash in some "each" closure you have to worry about it being a symbol or a string too.

Ruby is told to be programmers' friendly and it usually is. But symbols are certainly a big exception.


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

  parent reply	other threads:[~2013-02-07  1:57 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-06 13:50 [ruby-core:51898] [ruby-trunk - Feature #7792][Open] Make symbols and strings the same thing rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-06 14:13 ` [ruby-core:51905] [ruby-trunk - Feature #7792] " trans (Thomas Sawyer)
2013-02-06 14:18 ` [ruby-core:51907] Re: [ruby-trunk - Feature #7792][Open] " Yorick Peterse
2013-02-06 14:20 ` [ruby-core:51908] [ruby-trunk - Feature #7792] " luislavena (Luis Lavena)
2013-02-06 14:22 ` [ruby-core:51910] " shyouhei (Shyouhei Urabe)
2013-02-06 14:29 ` [ruby-core:51913] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-06 14:33 ` [ruby-core:51914] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-06 14:36   ` [ruby-core:51916] " Yorick Peterse
2013-02-06 14:51     ` [ruby-core:51920] " Rodrigo Rosenfeld Rosas
2013-02-06 15:25       ` [ruby-core:51926] " Yorick Peterse
2013-02-06 17:06         ` [ruby-core:51936] " Rodrigo Rosenfeld Rosas
2013-02-06 18:22           ` [ruby-core:51940] " Yorick Peterse
2013-02-06 18:50             ` [ruby-core:51941] " Rodrigo Rosenfeld Rosas
2013-02-06 19:12               ` [ruby-core:51943] " Yorick Peterse
2013-02-06 23:45                 ` [ruby-core:51953] " Rodrigo Rosenfeld Rosas
2013-02-07  0:54                   ` [ruby-core:51956] " David MacMahon
2013-02-06 14:58 ` [ruby-core:51922] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-06 20:19 ` [ruby-core:51946] [ruby-trunk - Feature #7792][Rejected] " drbrain (Eric Hodel)
2013-02-06 23:01 ` [ruby-core:51951] [ruby-trunk - Feature #7792] " phluid61 (Matthew Kerwin)
2013-02-06 23:51 ` [ruby-core:51954] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07  1:18 ` phluid61 (Matthew Kerwin) [this message]
2013-02-07 10:46 ` [ruby-core:51972] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07 12:04   ` [ruby-core:51979] " Matthew Kerwin
2013-02-07 13:09     ` [ruby-core:51981] " Rodrigo Rosenfeld Rosas
2013-02-07 21:11       ` [ruby-core:52005] " Matthew Kerwin
2013-02-07 22:17         ` [ruby-core:52008] " Rodrigo Rosenfeld Rosas
2013-02-07 23:19           ` [ruby-core:52014] " Matthew Kerwin
2013-02-07 10:50 ` [ruby-core:51973] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07 11:00 ` [ruby-core:51974] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07 17:34   ` [ruby-core:51994] " David MacMahon
2013-02-07 11:05 ` [ruby-core:51975] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07 17:01 ` [ruby-core:51993] " jeremyevans0 (Jeremy Evans)
2013-02-07 21:15   ` [ruby-core:52006] " Matthew Kerwin
2013-02-07 22:22     ` [ruby-core:52009] " Rodrigo Rosenfeld Rosas
2013-02-07 23:58       ` [ruby-core:52016] " Yukihiro Matsumoto
2013-02-08  0:26         ` [ruby-core:52017] " Rodrigo Rosenfeld Rosas
2013-02-08  1:16           ` [ruby-core:52019] " Yukihiro Matsumoto
2013-02-08  1:57           ` [ruby-core:52021] " Bill Kelly
2013-02-08  7:07             ` [ruby-core:52030] " Rodrigo Rosenfeld Rosas
2013-02-07 18:23 ` [ruby-core:51996] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07 18:43   ` [ruby-core:51997] " David MacMahon
2013-02-07 19:04     ` [ruby-core:51999] " Rodrigo Rosenfeld Rosas
2013-02-07 19:27       ` [ruby-core:52000] " David MacMahon
2013-02-08  1:15     ` [ruby-core:52018] " Eric Hodel
2013-02-08  1:37       ` [ruby-core:52020] " David MacMahon
2013-02-09  0:50         ` [ruby-core:52055] " Eric Hodel
2013-02-12  0:02           ` [ruby-core:52135] " David MacMahon
2013-02-07 18:58 ` [ruby-core:51998] " trans (Thomas Sawyer)
2013-02-07 19:28 ` [ruby-core:52001] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-02-07 19:59 ` [ruby-core:52002] " trans (Thomas Sawyer)
2013-02-07 20:08 ` [ruby-core:52004] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-03-20 15:02 ` [ruby-core:53576] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-03-21  3:10 ` [ruby-core:53595] " Student (Nathan Zook)
2013-03-21 13:01 ` [ruby-core:53604] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-03-21 13:04 ` [ruby-core:53605] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-03-25 15:14 ` [ruby-core:53722] " Student (Nathan Zook)
2013-03-26 20:46 ` [ruby-core:53749] " alexeymuranov (Alexey Muranov)
2013-03-29 17:34 ` [ruby-core:53826] " Student (Nathan Zook)
2013-04-03 10:06 ` [ruby-core:53952] " wardrop (Tom Wardrop)
2013-04-03 13:25   ` [ruby-core:53962] " Bill Kelly
2013-04-03 10:44 ` [ruby-core:53956] " phluid61 (Matthew Kerwin)
2013-04-03 12:01 ` [ruby-core:53960] " wardrop (Tom Wardrop)
2013-04-03 22:43 ` [ruby-core:53973] " wardrop (Tom Wardrop)
2014-07-15 15:39 ` [ruby-core:63742] " andrewm.bpi
2014-08-08 20:02 ` [ruby-core:64273] " eloyesp
2017-02-16  1:22 ` [ruby-core:79546] [Ruby trunk Feature#7792] " subtileos
2017-02-16  2:21 ` [ruby-core:79548] " billk
2017-02-16  2:33 ` [ruby-core:79549] " subtileos
2017-10-05 18:19 ` [ruby-core:83130] " sheerun
2017-10-07  4:15 ` [ruby-core:83172] " shevegen
2017-12-26  8:36 ` [ruby-core:84467] " danieldasilvaferreira
2017-12-26  8:52   ` [ruby-core:84470] " Eric Wong
2017-12-26  9:12     ` [ruby-core:84474] " Daniel Ferreira
2017-12-26  9:22       ` [ruby-core:84475] " Eric Wong
2017-12-26  8:43 ` [ruby-core:84468] " danieldasilvaferreira
2017-12-26  9:24 ` [ruby-core:84477] " danieldasilvaferreira
2017-12-26 14:33 ` [ruby-core:84479] " ko1
2017-12-26 15:11 ` [ruby-core:84480] " danieldasilvaferreira
2017-12-26 15:43 ` [ruby-core:84481] " ko1
2017-12-26 19:30 ` [ruby-core:84483] " danieldasilvaferreira
2017-12-26 21:45 ` [ruby-core:84488] " merch-redmine
2017-12-26 22:11 ` [ruby-core:84489] " danieldasilvaferreira
2017-12-27 17:35 ` [ruby-core:84521] " merch-redmine
2017-12-27 19:52 ` [ruby-core:84525] " danieldasilvaferreira
2017-12-27 20:29 ` [ruby-core:84528] " andrewm.bpi
2017-12-27 20:39 ` [ruby-core:84529] " danieldasilvaferreira

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-35957.20130207101822@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).