相关疑难解决方法(0)

如何使用单个字符串键访问散列的嵌套元素?

我在玩弄Ruby而且基本上都是这样

@trans = { :links => {
    :quick_notes => "aaaaaaa"
  }
}
Run Code Online (Sandbox Code Playgroud)

我想打电话

def t
  #...something
end
t('links.quick_notes')
Run Code Online (Sandbox Code Playgroud)

访问

trans[:links][:quick_notes]
Run Code Online (Sandbox Code Playgroud)

我基本上试图实现与使用国际化时相同的功能

I18n.t('something.other.foo') 
Run Code Online (Sandbox Code Playgroud)

我想出了这种方法

 def t(key)
   a=''
   key.to_s.split('.').each{|key|  a+="[:#{key}]" } 
   #now a == "[:links][:quick_notes]"
   #but I cant figure out how can I call it on  @trans variable

 end

 t('links.quick_notes')
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?感谢名单

ruby hash

9
推荐指数
1
解决办法
2109
查看次数

标签 统计

hash ×1

ruby ×1