mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
22 lines
529 B
Markdown
22 lines
529 B
Markdown
# Hash Slicing
|
|
|
|
Rails' ActiveSupport adds
|
|
[`#slice`](http://api.rubyonrails.org/classes/Hash.html#method-i-slice) and
|
|
[`#slice!`](http://api.rubyonrails.org/classes/Hash.html#method-i-slice-21)
|
|
to the `Hash` class. The interface of these two methods seems a little
|
|
inconsistent though.
|
|
|
|
```ruby
|
|
> {a: 1, b: 2, c: 3}.slice(:a)
|
|
=> {:a=>1}
|
|
```
|
|
|
|
The `#slice` method returns what is being sliced.
|
|
|
|
```ruby
|
|
> {a: 1, b: 2, c: 3}.slice!(:a)
|
|
=> {:b=>2, :c=>3}
|
|
```
|
|
|
|
The `#slice!` method, on the other hand, returns what is being excluded.
|