ActiveSupport::OrderedOptionsを使ってHashで.(ドット)を使えるようにする٩( ‘ω’ )و
もはやActiveSupportなしでは生きていけない僕
Hashをイイ感じに拡張してるActiveSupport::OrderedOptionsです
# activesupport/lib/active_support/ordered_options.rb module ActiveSupport class OrderedOptions < Hash ... end end
普通のHashだとこんな感じ
hash = {} hash[:name] = 'murajun1978' hash[:name] #=> 'murajun1978'
ActiveSupport::OrderedOptionsを使うとこうなる
hash = ActiveSupport::OrderedOptions.new hash.name = 'murajun1978' hash.name #=> 'murajun1978' hash[:name] #=> 'murajun1978'
.(ドット)でvalueを取得できるようになりましたーヾ( ̄∇ ̄=ノ
チェインしたいときはこんなかんじ
hash = ActiveSupport::OrderedOptions.new hash.user = ActiveSupport::OrderedOptions.new hash.user.name = 'murajun1978' hash.user.name #=> 'murajun1978'
既存のHashを使いたいときはActiveSupport:: InheritableOptionsを使う
hash = ActiveSupport:: InheritableOptions.new({name: 'murajun1978'}) hash.name #=> 'murajun1978'
I18nのconfigを定義するとこで使われてるねー
module I18n class Railtie < Rails::Railtie config.i18n = ActiveSupport::OrderedOptions.new config.i18n.railties_load_path = [] config.i18n.load_path = [] config.i18n.fallbacks = ActiveSupport::OrderedOptions.new ... end end
次はThreadについて書くよてい(多分、明日の神戸.rb Meetup #15で)
Happy Hacking٩( ‘ω’ )و