0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hash#sliceでkeyによるフィルタリング

Last updated at Posted at 2019-12-17

やりたかったこと

タイトルの通り、Hashをキーの配列でフィルタリングしたかった

want_to_do.rb
base_hash = {
  alpha: "α",
  beta: "β",
  gamma: "γ"
}

filters = %i[alpha gamma]

puts base_hash.somefunction filters # => { alpha: "α", gamma: "γ" }

みつけた

found_how_to_do.rb

# filters は * で展開してあげよう
base_hash.slice(*filters) # => { alpha: "α", gamma: "γ" }

結論

filterという言葉にとらわれ過ぎて、Enumerable#filterばかり当たってしまった。検索は多角的にやろう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?