How to rename a hash key in Ruby
Assume you have a Ruby Hash with the regions of the United States:
$ usa_regions
=> {"northeast"=>11, "southeast"=>12, "midwest"=>12, "southwest"=>4, "rest"=>9}But instead of calling the West the rest, you want to rename it to west.
Here’s how you would do that:
$ usa_regions["west"] = usa_regions.delete("rest")Now, usa_regions has a hash key named west instead of rest. Importantly, the value of rest – 9 – has been preserved and assigned to west:
$ usa_regions
=> {"northeast"=>11, "southeast"=>12, "midwest"=>12, "southwest"=>4, "west"=>9}