Class: Vagrant::Util::HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/vagrant/util/hash_with_indifferent_access.rb

Overview

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'

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, &block) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



16
17
18
19
20
21
22
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 16

def initialize(hash={}, &block)
  super(&block)

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

Instance Method Details

#[](key) ⇒ Object



24
25
26
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 24

def [](key)
  super(convert_key(key))
end

#[]=(key, value) ⇒ Object



28
29
30
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 28

def []=(key, value)
  super(convert_key(key), value)
end

#delete(key) ⇒ Object



32
33
34
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 32

def delete(key)
  super(convert_key(key))
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


51
52
53
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 51

def key?(key)
  super(convert_key(key))
end

#merge(other) ⇒ Object



40
41
42
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 40

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ Object



44
45
46
47
48
49
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 44

def merge!(other)
  other.each do |key, value|
    self[convert_key(key)] = value
  end
  self
end

#values_at(*indices) ⇒ Object



36
37
38
# File 'lib/vagrant/util/hash_with_indifferent_access.rb', line 36

def values_at(*indices)
  indices.collect { |key| self[convert_key(key)] }
end