Access Ruby Hash Using Dotted Path Key String

后端 未结 7 2226
情深已故
情深已故 2021-01-01 21:35

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\         


        
7条回答
  •  悲&欢浪女
    2021-01-01 22:19

    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')
    

提交回复
热议问题