How to replace a hash key with another key

后端 未结 11 1499
青春惊慌失措
青春惊慌失措 2020-12-04 07:36

I have a condition where, I get a hash

  hash = {\"_id\"=>\"4de7140772f8be03da000018\", .....}

and I want this hash as

         


        
11条回答
  •  甜味超标
    2020-12-04 08:10

    Answering exactly what was asked:

    hash = {"_id"=>"4de7140772f8be03da000018"}
    hash.transform_keys { |key| key[1..] }
    # => {"id"=>"4de7140772f8be03da000018"}
    

    The method transform_keys exists in the Hash class since Ruby version 2.5.

    https://blog.bigbinary.com/2018/01/09/ruby-2-5-adds-hash-transform_keys-method.html

提交回复
热议问题