15
8

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.

RailsでHashの一部だけを取り出す・取り除く方法

Posted at

ActiveSupportには

  • Hashの一部を取り出す: Hash#slice
  • Hashの一部を取り除く: Hash#except

ためのメソッドが用意されています.

以下の様に用います.

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

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

モデルの属性値の一部をinitializeなどで渡したいときに便利ですね.

参考

15
8
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
15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?