From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id BE89A1F9FD for ; Sun, 28 Mar 2021 01:56:54 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 4D29D120A00; Sun, 28 Mar 2021 10:55:54 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2F9301209E7 for ; Sun, 28 Mar 2021 10:55:51 +0900 (JST) Received: by filterdrecv-p3iad2-7d7c446bd4-zhfnd with SMTP id filterdrecv-p3iad2-7d7c446bd4-zhfnd-18-605FE25E-29 2021-03-28 01:56:46.641840146 +0000 UTC m=+372228.346202505 Received: from herokuapp.com (unknown) by ismtpd0148p1iad2.sendgrid.net (SG) with ESMTP id kcIPOWr9S8Ojex94Gwy3pA for ; Sun, 28 Mar 2021 01:56:46.567 +0000 (UTC) Date: Sun, 28 Mar 2021 01:56:46 +0000 (UTC) From: duerst@it.aoyama.ac.jp Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79101 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17472 X-Redmine-Issue-Author: naruse X-Redmine-Sender: duerst X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?uQY=2F2xNrNfHHTWbKn6MBvvzfU5Pqk9I4lnOVb0CFDuu64BRIh++ZEqf0yQnZJs?= =?us-ascii?Q?C46HNpxD1zx=2FVE6E1Ng=2FMunQsWhnJ7bxDzteeAF?= =?us-ascii?Q?X36GtpmUCCCNZj1XvY0BJTGYqEz78o8LS6w0dsB?= =?us-ascii?Q?4ohl+e90iDdkClbeHpHj7MKVtoyS5T162hyL44e?= =?us-ascii?Q?nfNv85ekEemZN4L4o8XxsvCamEHqqEovVc1lXBJ?= =?us-ascii?Q?1xJgXnF3mU5nY8ipU=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103066 Subject: [ruby-core:103066] [Ruby master Feature#17472] HashWithIndifferentAccess like Hash extension X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17472 has been updated by duerst (Martin D=FCrst). joelb (Joel Blum) wrote in #note-19: > > Use JSON.parse(data, symbolize_names: true) > = > I know that. Yet the fact is these bugs happen again and again (not only = to new Ruby devs, would you agree it's quite easy to forget to symbolize_ke= ys or stringify or what have you). I don't know if this suggestion is the r= ight solution for the problem but I was hoping we could at least agree ther= e's a problem for a sizeable segment of Ruby users. Maybe we should change the default on JSON.parse? That would probably lead = to too much backwards compatibility issues. Maybe we should introduce a new method where symbol keys are the default. T= hat could be done without backwards compatibility issues, just by spreading= the word to use the new method. Another option may be a global setting that projects could use to change th= e default. > A small thought: I think the original sin in Ruby was making name in {nam= e: 'joe'} a symbol, e.g have the new "javascript snytax" use symbol keys. No. Using a symbol is this location is the right thing to do in Ruby. Havin= g `:name` be a symbol, but `name:` be a string would be highly confusing. A= nd keys (i.e. member names) in data structures usually are identifiers rath= er than data, so in Ruby, symbols are more appropriate. If you really want an "original sin", it's that Ruby distinguishes between = identifiers (symbols) and strings (see also below). > I think it would have been better if it was a string / frozen string inst= ead. And if a developer really needed symbol keys (which us Rails devs actu= ally never do), he could have perhaps written {:name =3D> 'joe'} or whateve= r. And yes it means we would have had to access the hash so: ` hsh['name']`= and not `hsh[:name]` (I kinda like the latter for saving a character) but = we wouldn't have been having this discussion over and over again. By defaul= t hash keys would have been strings and that's that (correct me if I'm wron= g but that's how it is in most programming languages; I don't see js or pyt= hon devs insisting on symbol hash keys). Javascript doesn't have symbols in the first place, so I don't see how JS d= evs could insist on symbol hash keys anyway. I also haven't found symbols i= n Python, so I guess the same thing applies there, too. When searching for = "Python symbol", the only stuff I get is sympy. To see whether it's more na= tural to treat JSON keys as symbols or as strings, you would have to look a= t other languages that distinguish symbols and strings, e.g. Lisp. > I don't know if anyone would agree with me here, and I know it's too late= to do that anyway, but maybe it's worth mentioning. Different programming languages just handle different things differently, n= ot only in syntax but also in semantics. If you work with Javascript and Ru= by, you have to deal with the fact that in conditions, the empty string and= 0 are treated differently. There are other issues that you have to be awar= e of. Unfortunately, no way of getting around this. ---------------------------------------- Feature #17472: HashWithIndifferentAccess like Hash extension https://bugs.ruby-lang.org/issues/17472#change-91127 * Author: naruse (Yui NARUSE) * Status: Open * Priority: Normal * Target version: 3.1 ---------------------------------------- Rails has [ActiveSupport::HashWithIndifferentAccess](https://api.rubyonrail= s.org/classes/ActiveSupport/HashWithIndifferentAccess.html), which is widel= y used in Rails to handle Request, Session, ActionView's form construction,= ActiveRecord's DB communication, and so on. It receives String or Symbol a= nd normalize them to fetch the value. But it is implemented with Ruby. If w= e provide C implementation of that, Rails will gain the performance improve= ment. summary of previous discussion: https://github.com/rails/rails/pull/40182#i= ssuecomment-687607812 -- = https://bugs.ruby-lang.org/ Unsubscribe: