跳至内容 跳至搜索

可继承选项

InheritableOptions 提供了一个构造函数,用于构建一个从另一个哈希继承的 OrderedOptions 哈希。

如果您已经拥有一个哈希,并且想要基于它创建一个新的哈希,请使用此方法。

h = ActiveSupport::InheritableOptions.new({ girl: 'Mary', boy: 'John' })
h.girl # => 'Mary'
h.boy  # => 'John'

如果现有哈希具有字符串键,请在它上面调用 Hash#symbolize_keys

h = ActiveSupport::InheritableOptions.new({ 'girl' => 'Mary', 'boy' => 'John' }.symbolize_keys)
h.girl # => 'Mary'
h.boy  # => 'John'
方法
I
N

类公共方法

new(parent = nil)

# File activesupport/lib/active_support/ordered_options.rb, line 94
def initialize(parent = nil)
  if parent.kind_of?(OrderedOptions)
    # use the faster _get when dealing with OrderedOptions
    super() { |h, k| parent._get(k) }
  elsif parent
    super() { |h, k| parent[k] }
  else
    super()
  end
end

实例公共方法

inheritable_copy()

# File activesupport/lib/active_support/ordered_options.rb, line 105
def inheritable_copy
  self.class.new(self)
end