Ruby 6-3-2

こんにちは!

残留争い、真っただ中。

ハッシュ

ハッシュの続きです。

hash_delete.rb
Image from Gyazo

Image from Gyazo

指定のキーを削除するdeleteメソッドです。
返り値は削除された要素です。

hash_delete_if.rb
Image from Gyazo

Image from Gyazo

条件によって要素を削除するdelete_ifメソッドです。

hash_clear.rb
Image from Gyazo

Image from Gyazo

すべての要素を破棄するclearメソッドです。

hash_each.rb
Image from Gyazo

Image from Gyazo

各種のeachメソッドです。

hash_all.rb
Image from Gyazo

Image from Gyazo

ブロックに渡すパラメーターはキーと値の2種が必要です。

hash_merge.rb
Image from Gyazo

Image from Gyazo

ハッシュを結合するmergeメソッドです。
キーが重複した場合は上書きを行うため、ブロックを使った工夫が可能です。

hash_transform.rb
Image from Gyazo

Image from Gyazo

キーや値を加工するtransform_keysメソッド、transform_valuesメソッドです。

hash_invert.rb
Image from Gyazo

Image from Gyazo

キーと値を反転させるinvertメソッドです。
重複した場合、後者の値が優先されます。

hash_identity.rb
Image from Gyazo

Image from Gyazo

最後にcompare_by_identityメソッドで同一性を確認しました。

ハッシュの基本はここまでです。

ではでは。