Project

General

Profile

Actions

Feature #15822

closed

Add Hash#except

Added by timoschilling (Timo Schilling) almost 5 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:92538]

Description

According to Hash#slice, I would like to add Hash#except. Same for ENV.except.


Files

hash-except.patch (4.05 KB) hash-except.patch timoschilling (Timo Schilling), 05/06/2019 06:26 PM

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #13563: Implement Hash#choice method.ClosedActions
Is duplicate of Ruby master - Feature #8499: Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupportClosedmatz (Yukihiro Matsumoto)Actions
Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • Is duplicate of Feature #8499: Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport added
Actions #2

Updated by timoschilling (Timo Schilling) almost 5 years ago

  • File deleted (hash-except.patch)
Actions #4

Updated by matz (Yukihiro Matsumoto) over 4 years ago

Updated by matz (Yukihiro Matsumoto) over 4 years ago

Hash#slice was introduced via discussion in #13563. We didn't see the need for Hash#except yet. Any (real world use-case)?
I don't think the name except is the best name for the behavior.

Matz.

Updated by zverok (Victor Shepelev) over 4 years ago

@matz (Yukihiro Matsumoto) About use-cases, there are multiple, like:

  • Logging/debug printing some hash with sensitive or too large data excluded:

    puts "REQUEST: #{request_data.except(:body, :apikey)}
    
  • Data tidying:

    CSV.read('data.csv', headers: true).map(&:to_h).map { |row| OpenStruct.new(row.except("Manager comment")) }
    
  • Data conversion:

    paragraphs.map { |para| ParaStats.new(para.to_h.except(:text)) }
    

...and so on (without even mentioning working with models in Rails, where it is used extensively to, for example, copy model without its id and timestamps).
Redmine's sources, for example, make some use of it:

$ grep -F ".except(" {app,lib} -r | wc -l
27

I recently stumbled upon it, too.

About the name: I understand "ActiveSupport uses this name" is kinda lazy one, but it does :) And, TBH, for me (even before I started working with Rails, I used Ruby for first my 12 years without ActiveSupport) it always felt natural: request.except(:path, :body) requires no further explanations. Facets uses the name, too.

The alternative method name I can think of is, maybe, descriptive without_keys(*keys) (or except_keys, or just without(*keys)), but I am not sure it is any better.

Updated by matz (Yukihiro Matsumoto) almost 4 years ago

You have provided the use-case, and I understand the need. It's tempting the name exclude but except is OK too.
Accepted.

Matz.

Actions #8

Updated by timoschilling (Timo Schilling) over 3 years ago

  • Status changed from Open to Closed

Applied in changeset git|82ca8c73034b0a522fd2970ea39edfcd801955fe.


Add Hash#except ENV#except [Feature #15822]

Updated by marcandre (Marc-Andre Lafortune) over 3 years ago

I think except should always return a Hash, since Matz prefers returning the base class instead of subclass and we're changing a bunch of String methods too.

I will merge https://github.com/ruby/ruby/pull/3929 in a day or two unless there's an objection.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0