class Vagrant::Util::HashWithIndifferentAccess

A hash with indifferent access. Mostly taken from Thor/Rails (thanks). Normally I’m not a fan of using an indifferent access hash since Symbols are basically memory leaks in Ruby, but since Vagrant is typically a quick one-off binary run and it doesn’t use too many hash keys where this is used, the effect should be minimal.

hash[:foo]  #=> 'bar'
hash['foo'] #=> 'bar'

Public Class Methods

new(hash={}, &block) click to toggle source
Calls superclass method
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 13
def initialize(hash={}, &block)
  super(&block)

  hash.each do |key, value|
    self[convert_key(key)] = value
  end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 21
def [](key)
  super(convert_key(key))
end
[]=(key, value) click to toggle source
Calls superclass method
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 25
def []=(key, value)
  super(convert_key(key), value)
end
delete(key) click to toggle source
Calls superclass method
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 29
def delete(key)
  super(convert_key(key))
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source
Calls superclass method
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 48
def key?(key)
  super(convert_key(key))
end
Also aliased as: include?, has_key?, member?
member?(key)
Alias for: key?
merge(other) click to toggle source
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 37
def merge(other)
  dup.merge!(other)
end
merge!(other) click to toggle source
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 41
def merge!(other)
  other.each do |key, value|
    self[convert_key(key)] = value
  end
  self
end
values_at(*indices) click to toggle source
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 33
def values_at(*indices)
  indices.collect { |key| self[convert_key(key)] }
end

Protected Instance Methods

convert_key(key) click to toggle source
# File lib/vagrant/util/hash_with_indifferent_access.rb, line 58
def convert_key(key)
  key.is_a?(Symbol) ? key.to_s : key
end