有序选项¶ ↑
OrderedOptions 继承自 Hash 并提供动态访问器方法。
使用 Hash 时,键值对通常管理如下:
h = {} h[:boy] = 'John' h[:girl] = 'Mary' h[:boy] # => 'John' h[:girl] # => 'Mary' h[:dog] # => nil
使用 OrderedOptions,上述代码可以这样编写:
h = ActiveSupport::OrderedOptions.new h.boy = 'John' h.girl = 'Mary' h.boy # => 'John' h.girl # => 'Mary' h.dog # => nil
若要在值为空时引发异常,请在键名后附加感叹号,例如:
h.dog! # => raises KeyError: :dog is blank
方法
- #
- D
- E
- I
- M
- R
实例公共方法
[](key) 链接
也别名为:_get
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 41 def [](key) super(key.to_sym) end
[]=(key, value) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 37 def []=(key, value) super(key.to_sym, value) end
dig(key, *identifiers) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 45 def dig(key, *identifiers) super(key.to_sym, *identifiers) end
extractable_options?() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 64 def extractable_options? true end
inspect() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 68 def inspect "#<#{self.class.name} #{super}>" end
method_missing(method, *args) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 49 def method_missing(method, *args) if method.end_with?("=") self[method.name.chomp("=")] = args.first elsif method.end_with?("!") name_string = method.name.chomp("!") self[name_string].presence || raise(KeyError.new(":#{name_string} is blank")) else self[method.name] end end
respond_to_missing?(name, include_private) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/ordered_options.rb, line 60 def respond_to_missing?(name, include_private) true end