ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mail@timcraft.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:102217] [Ruby master Feature#17472] HashWithIndifferentAccess like Hash extension
Date: Sat, 23 Jan 2021 11:54:50 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-90064.20210123115449.5@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17472.20201226110418.5@ruby-lang.org

Issue #17472 has been updated by timcraft (Tim Craft).


hcatlin (Hampton Catlin) wrote in #note-15:
> Would anyone here think the code below was acceptable?

Mixing symbol keys and string keys together like that would no doubt be very confusing, but why would you choose to do that instead of just using symbol keys or string keys? With modern Ruby the use case handled by options hashes is typically better handled by keyword arguments, and sometimes an options/config object might be a more appropriate choice. Keyword argument hashes use symbol keys, and I would expect options/config objects to be implemented using symbol keys.

We now also have Hash#transform_keys, so transforming a hash with string keys from outside data sources to symbol keys is straightforward. That can easily handle use cases like transforming keys in config/database.yml to symbols. JSON has symbolize_names and so on. There doesn't need to be confusion if library authors make a clear decision on whether to use strings or symbols for a specific use case, instead of trying to support both.

> And this is why Rails has HashWithIndifferentAccess and most of us would prefer to use it in our libraries, and just don't' want to have the awkwardness of having to type out that horribly long name every time.

Maybe I'm in the minority, but I would never use HashWithIndifferentAccess in my own libraries, and I try hard not to use it directly in Rails because I've seen it become the source of confusion and subtle bugs!

I think the critical use case for HashWithIndifferentAccess is params; where you want to be using symbol keys because it's cleaner syntax, but the keys are coming from untrusted input. But HashWithIndifferentAccess is just the historical solution that exists for that case. There's no reason there couldn't be an ActiveSupport::Params class which allowed for `params.title` instead of `params[:title]`, making the choice between whether to use string keys or symbol keys disappear (in the calling code at least).

> In almost every use of Hash in my career, the keys have being either symbols or strings, and I can't think of production code that even uses the fact that you can use an object as a key as a feature. I think I've attempted it myself a couple times, but usually refactored the code after, berating myself for trying to be a little too clever.

I have a lot of code which does, plenty of it in production. Hashes that represent a set of attributes, hashes that represent a set of variables (e.g. for template locals), and keyword argument hashes are relatively frequent examples for using symbol keys. The syntax for hash literals with symbol keys is so much nicer to read and write than the hashrocket syntax with string keys.

Apart from symbols the other common use case I encounter is time series data. I frequently use date keys, integer keys for years, month keys, and quarter keys. Having this as an option instead of only being able to use strings is one of the reasons I prefer Ruby to other languages. And I don't consider that kind of code to be clever. Quite the opposite, it feels like "boring" code which just works.

> In the last year I've come back to full time Ruby programming, and this remains one of my biggest frustrations with the language.

Welcome back to Ruby! :) Are you working on Rails apps? If so it seems a little unfair to get frustrated at the language for choices made by the web framework. If not can you share an example of a Ruby use case that would be significantly improved by HashWithIndifferentAccess?

----

FWIW I'm against this proposal for the reasons already mentioned:

* The name is terrible, which apart from making it clumsy to use and not seeming very Ruby-ish I think demonstrates that the semantics of the class are a bit unclear. As Jeremy points out it is not indifferent in many respects, just with symbols and strings.

* There isn't any profiling data which shows this is a significant bottleneck for a significant number of Rails projects. Optimizing performance for Date, Time, BigDecimal, JSON, URI, YAML etc would benefit Rails *and* a broader range of Ruby applications.

* Re-implementing in C adds more work for maintainers of alternative Ruby implementations, for seemingly little benefit.

The original motivation here seems to be for improving performance of Rails applications. Why does it need to be in Ruby core/stdlib? Why can't it just be implemented as a C extension packaged in a gem?


----------------------------------------
Feature #17472: HashWithIndifferentAccess like Hash extension
https://bugs.ruby-lang.org/issues/17472#change-90064

* Author: naruse (Yui NARUSE)
* Status: Open
* Priority: Normal
* Target version: 3.1
----------------------------------------
Rails has [ActiveSupport::HashWithIndifferentAccess](https://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html), which is widely used in Rails to handle Request, Session, ActionView's form construction, ActiveRecord's DB communication, and so on. It receives String or Symbol and normalize them to fetch the value. But it is implemented with Ruby. If we provide C implementation of that, Rails will gain the performance improvement.

summary of previous discussion: https://github.com/rails/rails/pull/40182#issuecomment-687607812



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

  parent reply	other threads:[~2021-01-23 11:54 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-26 11:04 [ruby-core:101707] [Ruby master Feature#17472] HashWithIndifferentAccess like Hash extension naruse
2020-12-26 11:06 ` [ruby-core:101708] " ronnie
2020-12-26 12:53 ` [ruby-core:101713] " eregontp
2020-12-27 10:44 ` [ruby-core:101737] " mame
2020-12-27 16:14 ` [ruby-core:101747] " eregontp
2020-12-27 17:13 ` [ruby-core:101751] " merch-redmine
2020-12-27 19:09 ` [ruby-core:101755] " daniel
2020-12-27 20:08 ` [ruby-core:101758] " marcandre-ruby-core
2020-12-28  3:41 ` [ruby-core:101767] " naruse
2020-12-28 11:22 ` [ruby-core:101772] " jean.boussier
2020-12-28 11:24 ` [ruby-core:101773] " naruse
2020-12-28 11:53 ` [ruby-core:101774] " eregontp
2020-12-29 20:34 ` [ruby-core:101807] " zverok.offline
2021-01-02 10:53 ` [ruby-core:101877] " nobu
2021-01-17 12:57 ` [ruby-core:102122] " hcatlin
2021-01-23 11:54 ` mail [this message]
2021-03-27  0:31 ` [ruby-core:103046] " yoelblumenator
2021-03-27  1:03 ` [ruby-core:103048] " marcandre-ruby-core
2021-03-27  6:49 ` [ruby-core:103051] " yoelblumenator
2021-03-28  1:56 ` [ruby-core:103066] " duerst
2021-03-28  3:17 ` [ruby-core:103067] " marcandre-ruby-core
2021-03-28 17:29 ` [ruby-core:103075] " yoelblumenator
2021-03-31  0:19 ` [ruby-core:103119] " duerst
2021-04-01 21:49 ` [ruby-core:103161] " lamont
2021-04-02  4:11 ` [ruby-core:103163] " sawadatsuyoshi
2021-04-02 13:36 ` [ruby-core:103182] " yoelblumenator
2021-04-02 13:45 ` [ruby-core:103183] " yoelblumenator
2021-04-02 14:13 ` [ruby-core:103184] " daniel
2021-04-02 19:34 ` [ruby-core:103189] " lamont
2021-04-02 19:42 ` [ruby-core:103190] " lamont
2022-11-20  8:18 ` [ruby-core:110824] " naruse (Yui NARUSE)

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-90064.20210123115449.5@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).