Skip to content

Latest commit

 

History

History
21 lines (16 loc) · 529 Bytes

hash-slicing.md

File metadata and controls

21 lines (16 loc) · 529 Bytes

Hash Slicing

Rails' ActiveSupport adds #slice and #slice! to the Hash class. The interface of these two methods seems a little inconsistent though.

> {a: 1, b: 2, c: 3}.slice(:a)
=> {:a=>1}

The #slice method returns what is being sliced.

> {a: 1, b: 2, c: 3}.slice!(:a)
=> {:b=>2, :c=>3}

The #slice! method, on the other hand, returns what is being excluded.