Module: Kwalify::Util::HashLike

Included in:
HashInterface
Defined in:
lib/kwalify/util/hashlike.rb

Overview

add hash-like methods

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



16
17
18
# File 'lib/kwalify/util/hashlike.rb', line 16

def [](key)
  instance_variable_get("@#{key}")
end

#[]=(key, val) ⇒ Object



20
21
22
# File 'lib/kwalify/util/hashlike.rb', line 20

def []=(key, val)
  instance_variable_set("@#{key}", val)
end

#eachObject

not necessary



39
40
41
42
43
44
45
# File 'lib/kwalify/util/hashlike.rb', line 39

def each   # not necessary
  instance_variables().each do |name|
    key = name[1, name.length-1]
    val = instance_variable_get(name)
    yield(key, val)
  end
end

#key?(key) ⇒ Boolean

– def keys()

instance_variables().collect { |name| name[1, name.length-1] }

end ++

Returns:



30
31
32
# File 'lib/kwalify/util/hashlike.rb', line 30

def key?(key)
  instance_variables().include?("@#{key}")
end