本文介绍了如何替换具有'。'的所有散列键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用Ruby on Rails 4,并且想要替换所有散列键,以便从

I am using Ruby on Rails 4 and I would like to replace all hash keys so to change the hash from

h_before = {:"aaa.bbb" => 1, :c => 2, ...}

h_after = {:bbb => 1, :c => 2, ...}

也就是说,我想要去模块化所有散列键

That is, I would like to someway "demodulize" all hash keys having the .. How can I make that?

推荐答案

h_before = {:"aaa.bbb" => 1, :c => 2}
h_after =
h_before.inject({}){|h, (k, v)| h[k.to_s.split(".").last.to_sym] = v; h}
# => {:bbb = > 1, :c => 2}

这篇关于如何替换具有'。'的所有散列键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:51