1
0

More than 3 years have passed since last update.

【Ruby】merge, merge!

Posted at

mergeメソッドについて

2つのハッシュを統合するレシーバhashと引数other_hashのキーと値を集め、新しいハッシュに入れて返す。重複するキーが有るときは、ohter_hashの値が使われます。

image1 = { :src => "monkey.jpg", :alt => "monkey" }
image2 = { :width => 320, :height => 240 }
image3 = image1.merge(image2)
puts image3[:src], image3[:width]
monkey.jpg
320

使い方

hash.merge(other_hash) {|key, oldval, newval| block }
1
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
1
0