[ruby-core:95394] [Ruby master Feature#15822] Add Hash#except
From:
zverok.offline@...
Date:
2019-10-17 14:32:15 UTC
List:
ruby-core #95394
Issue #15822 has been updated by zverok (Victor Shepelev).
@matz About use-cases, there are multiple, like:
* Logging/debug printing some hash with sensitive or too large data excluded:
```ruby
puts "REQUEST: #{request_data.except(:body, :apikey)}
```
* Data tidying:
```ruby
CSV.read('data.csv', headers: true).map(&:to_h).map { |row| OpenStruct.new(row.except("Manager comment")) }
```
* Data conversion:
```ruby
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:
```bash
$ grep -F ".except(" {app,lib} -r | wc -l
27
```
I recently [stumbled upon it, too](https://github.com/zverok/time_calc/blob/master/lib/time_calc/types.rb#L58).
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](https://www.rubydoc.info/github/rubyworks/facets/Hash#except-instance_method), 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.
----------------------------------------
Feature #15822: Add Hash#except
https://bugs.ruby-lang.org/issues/15822#change-82107
* Author: timoschilling (Timo Schilling)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
According to `Hash#slice`, I would like to add `Hash#except`. Same for `ENV.except`.
---Files--------------------------------
hash-except.patch (4.05 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>