Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/configurable/core_ext/extract_options.rb

Instance Method Summary collapse

Instance Method Details

#extractable_options?Boolean

By default, only instances of Hash itself are extractable. Subclasses of Hash may implement this method and return true to declare themselves as extractable. If a Hash is extractable, Array#extract_options! pops it from the Array when it is the last element of the Array.

Returns:

  • (Boolean)


37
38
39
# File 'lib/configurable/core_ext/extract_options.rb', line 37

def extractable_options?
  instance_of?(Hash)
end

#symbolize_keysObject



53
54
55
# File 'lib/configurable/core_ext/extract_options.rb', line 53

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object

Destructively convert all keys to symbols, as long as they respond to to_sym.



46
47
48
49
50
51
# File 'lib/configurable/core_ext/extract_options.rb', line 46

def symbolize_keys!
  keys.each do |key|
    self[(key.to_sym rescue key) || key] = delete(key)
  end
  self
end

#to_args(box = true) ⇒ Object

Puts the hash into an array suitable for expanding into a parameter list, such that it will be interpreted as keyword parameters if the method you’re calling expects that.



61
62
63
64
# File 'lib/configurable/core_ext/extract_options.rb', line 61

def to_args(box = true)
  args = self.symbolize_keys
  box ? [args] : args
end