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.

Rubyのhashでvalueの順番を明示的に取得する方法

Posted at

values_atメソッド

values_atメソッドを使えばvalueの順番を明示的に取得できる。

hash_values.rb
hash = { 'name' => 'yamada', 'gender' => 'm', 'age' => 20 }

p hash.values                                        #=> ["yamada", "m", 20]
p hash.values_at('name', 'gender', 'age')            #=> ["yamada", "m", 20]
p hash.values_at('age', 'name', 'gender')            #=> [20, "yamada", "m"]
p hash.values_at('name', 'gender', 'age', 'address') #=> ["yamada", "m", 20, nil]

valueの順番を入れ替えて表示することもできる。
存在しないkeyを指定するとnilが帰ってくる。

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?