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?

【Rails】Hashから特定の値を抽出・除外する方法

Posted at

Hash#slice

Hash#sliceメソッドは、指定したキーに対応する値だけを取り出すことができます。以下のように使用します。

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

この例では、ハッシュから:a:cのキーに対応する値だけを取り出しています。

Hash#except

一方、Hash#exceptメソッドは、指定したキーに対応する値を除外した新しいハッシュを返します。以下のように使用します。

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

この例では、:aのキーに対応する値を除外したハッシュを取得しています。

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?