Converting nested hash keys from CamelCase to snake_case in Ruby

后端 未结 6 463
一生所求
一生所求 2020-12-30 20:21

I\'m trying to build an API wrapper gem, and having issues with converting hash keys to a more Rubyish format from the JSON the API returns.

The JSON contains multip

6条回答
  •  独厮守ぢ
    2020-12-30 20:58

    If you're using the active_support library, you can use deep_transform_keys! like so:

    hash.deep_transform_keys! do |key|
      k = key.to_s.snakecase rescue key
      k.to_sym rescue key
    end
    

提交回复
热议问题