I access params from a controller.
I need to rename one of keys. key value to stay the same
Old name is :xx, value = ‘test’
New name is :yy, value = ‘test’
Simplest way to do this in one line of code?
I access params from a controller.
I need to rename one of keys. key value to stay the same
Old name is :xx, value = ‘test’
New name is :yy, value = ‘test’
Simplest way to do this in one line of code?
This is technically not one line but you could do.
my_var = {xx: “test”}
my_var[:yy] = my_var[:xx]; my_var.delete(:xx)
Matt
On Tue, Apr 8, 2014 at 2:28 PM, Matthew W. [email protected]
wrote:
This is technically not one line but you could do.
my_var = {xx: “test”}
my_var[:yy] = my_var[:xx]; my_var.delete(:xx)
It becomes one line by:
my_var[:yy] = my_var.delete(:xx)
as Hash#delete returns the value at the specified key, or else nil
if there was no :xx key.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs