Class: Rory::HashWithDubiousSemantics

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/rory/hash_with_dubious_semantics.rb

Overview

ActiveSupport’s HashWithIndifferentAccess put it best:

“This class has dubious semantics and we only have it so that people can write params instead of params and they get the same value for both keys.”

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashWithDubiousSemantics

Returns a new instance of HashWithDubiousSemantics.



10
11
12
13
14
# File 'lib/rory/hash_with_dubious_semantics.rb', line 10

def initialize(hash)
  fail ArgumentError unless hash.is_a?(Hash)
  hash_with_no_symbols = convert_hash(hash)
  super( hash_with_no_symbols )
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
19
# File 'lib/rory/hash_with_dubious_semantics.rb', line 16

def [](key)
  actual_key = convert_key(key)
  __getobj__[actual_key]
end

#[]=(key, value) ⇒ Object



21
22
23
24
25
# File 'lib/rory/hash_with_dubious_semantics.rb', line 21

def []=(key, value)
  actual_key = convert_key(key)
  new_value = convert_value(value)
  __getobj__[actual_key] = new_value
end

#inspectObject



27
28
29
# File 'lib/rory/hash_with_dubious_semantics.rb', line 27

def inspect
  "#<#{self.class.name} @hash=#{super}>"
end