【戻る】 【進む】   目次        36. 組み込みクラス - Hash

ハッシュ(Hash)

  • インデックス参照、代入 receipt = {"apple" => 250, "banana" => 170, "cranberry" => 200 } receipt["banana"] #=> 170 receipt["banana"] = 180 receipt["banana"] #=> 180
  • 削除関連 receipt = {"apple" => 250, "banana" => 170, "cranberry" => 200 } receipt.delete("banana") #=> bananaを削除 receipt #=> {"apple"=>250, "cranberry"=>200 } receipt = {"apple" => 250, "banana" => 170, "cranberry" => 200 } receipt.delete_if { |x, v| x == "apple" && v >= 250 } #=> appleの値が250以上だったら削除 receipt #=> {"banana" => 170, "cranberry"=>200 }
  • each receipt = {"apple" => 250, "banana" => 170, "cranberry" => 200 } receipt.each do |x, v| print x, "=>", v, "\n" end
  • 配列化 receipt = {"apple" => 250, "banana" => 170, "cranberry" => 200 } receipt.keys #=> ["apple", "banana", "cranberry"] receipt.values #=> [250, 170, 200]
  • size, empty? {"apple"=>250, "banana"=>170}.size #=> 2 p ({}.empty?) #=> true p ({""=>""}.empty?) #=> false

  • 【戻る】 【進む】   目次        36. 組み込みクラス - Hash