Accessing a Ruby hash with a variable as the key

后端 未结 2 1409
忘了有多久
忘了有多久 2021-01-17 12:10

If I had the following ruby hash:

environments = {
   \'testing\' =>  \'11.22.33.44\',
   \'production\' => \'55.66.77.88\'
}

How wou

2条回答
  •  独厮守ぢ
    2021-01-17 12:23

    You would use brackets:

    environments = {
       'testing' =>  '11.22.33.44',
       'production' => '55.66.77.88'
    }
    myString = 'testing'
    environments[myString] # => '11.22.33.44'
    

提交回复
热议问题