Access Ruby Hash Using Dotted Path Key String

2019-02-16 12:50发布

The Rails I18n library transforms a YAML file into a data structure that is accessible via a dotted path call using the t() function.

t('one.two.three.four')

Does anyone know how to do this with a Ruby Hash? Or is it only possible directly via a YAML object?

7条回答
SAY GOODBYE
2楼-- · 2019-02-16 13:26

There is a Gem too keypath-ruby

gem 'key_path', :git => 'https://github.com/nickcharlton/keypath-ruby.git'

Looking at the code (and guessing a little about what t is), it looks like you can do this:

t.value_at_keypath('one.two.three.four')
查看更多
登录 后发表回答