Spec-Zone.ru › Ruby on Rails 8.1

class ActiveSupport::OrderedOptions

Родительский класс:
Hash

Упорядоченные параметры

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

Открытые методы экземпляра

[] (key) Показать исходный код
# File activesupport/lib/active_support/ordered_options.rb, line 41
def [](key)
  super(key.to_sym)
end
Вызывает метод родительского класса
Также имеет псевдоним: _get
[]= (key, value) Показать исходный код
# File activesupport/lib/active_support/ordered_options.rb, line 37
def []=(key, value)
  super(key.to_sym, value)
end
Вызывает метод родительского класса
_get (key)
Псевдоним для: []
dig (key, *identifiers) Показать исходный код
# File activesupport/lib/active_support/ordered_options.rb, line 45
def dig(key, *identifiers)
  super(key.to_sym, *identifiers)
end
Вызывает метод родительского класса
extractable_options? () Показать исходный код
# File activesupport/lib/active_support/ordered_options.rb, line 64
def extractable_options?
  true
end
inspect () Показать исходный код
# File activesupport/lib/active_support/ordered_options.rb, line 68
def inspect
  "#<#{self.class.name} #{super}>"
end
method_missing (method, *args) Показать исходный код
# 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) Показать исходный код
# File activesupport/lib/active_support/ordered_options.rb, line 60
def respond_to_missing?(name, include_private)
  true
end

© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API