Initializing hashes

后端 未结 5 1500
傲寒
傲寒 2021-02-07 10:25

I frequently write something like this:

a_hash[\'x\'] ? a_hash[\'x\'] += \' some more text\' : a_hash[\'x\'] = \'first text\'

There ought to be

5条回答
  •  梦谈多话
    2021-02-07 11:02

    You can specify the initial value when you create your hash:

    a_hash = { 'x' => 'first text' }
    // ...
    a_hash['x'] << ' some more text'
    

提交回复
热议问题