How to add to an existing hash in Ruby

前端 未结 7 1613
悲哀的现实
悲哀的现实 2021-01-30 00:02

In regards to adding an key => value pair to an existing populated hash in Ruby, I\'m in the process of working through Apress\' Beginning Ruby and have just fi

相关标签:
7条回答
  • 2021-01-30 00:44

    You can use double splat operator which is available since Ruby 2.0:

    h = { a: 1, b: 2 }
    h = { **h, c: 3 }
    p h
    # => {:a=>1, :b=>2, :c=>3}
    
    0 讨论(0)
提交回复
热议问题