Feature #14680

Adding +@ and -@ to hash and array

Added by kddeisz (Kevin Deisz) 9 days ago. Updated 9 days ago.

Target version:


Since we have -@ and +@ for strings and it's very useful (and reads better than .freeze) I'd like to propose adding the same API to hash and array. Especially with constants, linters are always telling me to freeze them, and I'd like to be able to accomplish that with -@. I've attached the necessary code to make that happen.

freeze.patch (3.86 KB) freeze.patch kddeisz (Kevin Deisz), 04/11/2018 02:49 PM


#1 [ruby-core:86515] Updated by shevegen (Robert A. Heiler) 9 days ago

I like .dup and .freeze, more than + and - on class String.

The only good thing is that I don't have to use any of these
methods so ultimately I don't have to care either way. :)

As for linters, I assume you refer to rubocop since that is
by far the most common use case I could see, with people
putting things such as:

FOO = 'foo'.freeze
BAR = 'bar'.freeze

into files. I wonder if matz thinks that this is a good way;
to me, I always wondered when people do so. Probably
lint-based development ... should that not be superfluous
with frozen strings now? One comment on top of the file
and avoid all the explicit .freeze calls?

#2 [ruby-core:86516] Updated by kddeisz (Kevin Deisz) 9 days ago

I don't think it's as common these days to see .freeze all over the place, since the linters explicitly tell you to use the frozen string literal comment. But that's explicitly for literals. There are plenty of cases where you'd want to freeze strings without them being a literal in the code. In this case there's also plenty of occurrences where you'd want to freeze hashes and arrays without them beings literals. Maybe I should have rephrased the ticket, it's definitely not just for the benefit of lint-based development.

#3 Updated by nobu (Nobuyoshi Nakada) 9 days ago

  • Description updated (diff)
  • Subject changed from Adding @+ and @- to hash and array to Adding +@ and -@ to hash and array

Also available in: Atom PDF