ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: David MacMahon <davidm@astro.berkeley.edu>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:51997] Re: [ruby-trunk - Feature #7792] Make symbols and strings the same thing
Date: Fri, 8 Feb 2013 03:43:47 +0900	[thread overview]
Message-ID: <50948DC7-BA78-452F-AAA0-BF1104FE3D8B@astro.berkeley.edu> (raw)
In-Reply-To: <redmine.journal-36002.20130208032309@ruby-lang.org>


On Feb 7, 2013, at 10:23 AM, rosenfeld (Rodrigo Rosenfeld Rosas) wrote:

> 
> Issue #7792 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).
> 
> 
> david_macmahon (David MacMahon) wrote:
>> I still think the fundamental issue is that Sequel is returning something that contains symbols, but JSON.parse(JSON.unparse(x)) will never return anything containing a symbol even if x does.  Because of this the "users" variable is assigned something that contains symbols in one case and something that contains no symbols in all other cases.  IMHO, the example shows not a deficiency in the Ruby language itself but rather a (subtle, easy to fall victim to) coding pitfall of not properly managing/reconciling the incompatible return types from the two libraries.
> 
> But don't you agree that we wouldn't have this kind of problem if Symbols behave just like Strings? Or if Symbols didn't exist at all? Or even if hashes always behaved as HWIA?

Yes, I agree that we wouldn't have this kind of problem if any of those alternatives existed.  I'm just not (yet) convinced that any of those alternatives are desirable just to avoid this kind of problem.  I do somewhat like the idea of having Hash behave as HWIA.  I think the number of real world uses of something like:

{:a => 0, 'a' => 1}

is likely to be very small.

>>> I know the code above could be simplified, but I'm avoid the parse -> unparse operation when it is not required (although it would make the code always work in this case):
>>> 
>>> users = JSON.parse (cache['users'] ||= JSON.unparse db[:users].select(:id, :name).all)
>> 
>> This seems an excellent solution IMHO as it clearly results in "users" being assigned the return value of JSON.parse every time and only results in one extra JSON.parse call only on the first time through.  That seems a pretty small price to pay for reconciling the two libraries' incompatible return return types.
> 
> An extra parse operation on a big list could easily add 100ms to the request timing.

So use explicit initialization instead of lazy initialization.

> Also, it doesn't happen only the first time but on each request.

In the original example, JSON.parse was used on every call except the first one.  The modified example uses JSON.parse on every call including the first one.  That's why I said the modified example has only one extra JSON.parse call (i.e. the one extra one on the first call).  If that's too much overhead, either don't use lazy initialization or explicitly invoke the method once at startup to force lazy initialization so it doesn't impact the (un?)lucky first user.

Dave

  reply	other threads:[~2013-02-07 19:01 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 ` [ruby-core:51959] " phluid61 (Matthew Kerwin)
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   ` David MacMahon [this message]
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=50948DC7-BA78-452F-AAA0-BF1104FE3D8B@astro.berkeley.edu \
    --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).