How can I get JavaScript style hash access?

后端 未结 4 1786
余生分开走
余生分开走 2021-02-19 11:05

I am aware of this feature provided by ActiveSupport.

h = ActiveSupport::OrderedOptions.new
h.boy = \'John\'
h.girl = \'Mary\'
h.boy  # => \'John\'
h.girl # =         


        
4条回答
  •  广开言路
    2021-02-19 11:36

    I created my own gem for this, and I've been using it in all my projects. Seems to do just what you need:

    large_hash = { boy: 'John', girl: 'Mary' }
    r = Ribbon.wrap large_hash
    
    r.boy
     => "John"
    
    r.girl
     => "Mary"
    
    r.keys
     => [:boy, :girl]
    
    r.each_pair { |key, value| puts "#{key} => #{value}" }
    boy => John
    girl => Mary
    

    Would love some feedback.

提交回复
热议问题