どうも、ちょげ(@chogetarou)です。
ハッシュ(Hash)の特定のキー(key)の値(value)を変更する方法を紹介します。
方法

ハッシュ(Hash)の特定のキー(key)の値(value)を変更するには、[]を使います。
まず、「=」の左辺で、ハッシュ名と中にキーを指定した[]を記述します。
そして、「=」の右辺に新しい値を記述します。
#hash=対象のハッシュ, key=キー, newValue=新しい値
hash[key] = newValue
上記の代入で、ハッシュ(Hash)のキー(key)を新しい値(value)に変更できます。
使用例
numbers = {
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4,
"five" => 5,
}
numbers["one"] = -1
numbers["three"] = -3
p numbers
出力:
{"one"=>-1, "two"=>2, "three"=>-3, "four"=>4, "five"=>5}
コメント