How to add new item to hash

前端 未结 7 1220
余生分开走
余生分开走 2020-12-22 18:44

I\'m new to Ruby and don\'t know how to add new item to already existing hash. For example, first I construct hash:

hash = {item1: 1}

after

7条回答
  •  一向
    一向 (楼主)
    2020-12-22 19:05

    hash.store(key, value) - Stores a key-value pair in hash.

    Example:

    hash   #=> {"a"=>9, "b"=>200, "c"=>4}
    hash.store("d", 42) #=> 42
    hash   #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
    

    Documentation

提交回复
热议问题