[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77657] [Ruby trunk Feature#9108] Hash sub-selections
From:
tom@...
Date:
2016-10-18 03:55:29 UTC
List:
ruby-core #77657
Issue #9108 has been updated by Tom Wardrop.
Indeed, I'm still hanging out for this. Seems like such a common thing I run into, and I'm always surprised this functionality isn't built in. ```Hash#select { |k,v| {...}.include?(k) }``` is very verbose.
----------------------------------------
Feature #9108: Hash sub-selections
https://bugs.ruby-lang.org/issues/9108#change-60929
* Author: Tom Wardrop
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
Hi,
I seem to regularly have the requirement to work on a sub-set of key/value pairs within a hash. Ruby doesn't seem to provide a concise means of selecting a sub-set of keys from a hash. To give an example of what I mean, including how I currently achieve this:
```ruby
sounds = {dog: 'woof', cat: 'meow', mouse: 'squeak', horse: 'nay', cow: 'moo'}
domestic_sounds = sounds.select { |k,v| [:dog, :cat].include? k } #=> {dog: 'woof', cat: 'meow'}
```
I think a more concise and graceful solution to this would be to allow the Hash#[] method to take multiple arguments, returning a sub-hash, e.g.
```ruby
domestic_sounds = sounds[:dog, :cat] #=> {dog: 'woof', cat: 'meow'}
```
I had a requirement in the current project I'm working on to concatenate two values in a hash. If this proposed feature existed, I could of just done this...
```ruby
sounds[:dog, :cat].values.join #=> 'woofmeow'
```
You could do something similar for the setter also...
```ruby
sounds[:monkey, :bat] = 'screech'
sounds #=> {dog: 'woof', cat: 'meow', mouse: 'squeak', horse: 'nay', cow: 'moo', monkey: 'screech', bat: 'screech'}
```
Concise, convenient and readable. Thoughts?
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>