How to add new item to hash

前端 未结 7 1214
余生分开走
余生分开走 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:20

    It's as simple as:

    irb(main):001:0> hash = {:item1 => 1}
    => {:item1=>1}
    irb(main):002:0> hash[:item2] = 2
    => 2
    irb(main):003:0> hash
    => {:item1=>1, :item2=>2}
    
    0 讨论(0)
提交回复
热议问题