Class: Hash
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/hash.rb', line 10
def method_missing( m, *args )
case m[-1]
when '?'
n = m[0..-2].to_sym
self.key?(n) || self.key?(n.to_s)
when '='
self[ m[0..-2].to_sym ] = args.first
else
self[m] || self[m.to_s]
end
end
|
Instance Method Details
#add_keysym_from(h) ⇒ Object
4
5
6
7
|
# File 'lib/hash.rb', line 4
def add_keysym_from(h)
h.each{|k,v| self[ k.to_sym ] = v }
self
end
|
#any_key?(*list) ⇒ Boolean
54
55
56
|
# File 'lib/hash.rb', line 54
def any_key?(*list)
(( self.keys & list ).count > 0 ) && !! ( self.keys & list )
end
|
#deep_merge(src) ⇒ Object
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/hash.rb', line 39
def deep_merge(src)
dst = dup
dst.merge(src){|k, o1, o2|
if o1.is_a?(o2.class) && o1.respond_to?(:deep_merge)
o1.deep_merge(o2)
else o2.dup
end
}
end
|
#filter_keys(*kk) ⇒ Object
58
59
60
|
# File 'lib/hash.rb', line 58
def filter_keys( *kk )
filter{ |k, v| kk.include?(k) }
end
|
#keys?(*list) ⇒ Boolean
50
51
52
|
# File 'lib/hash.rb', line 50
def keys?(*list)
keys.include?(*list)
end
|
#keys_to_symbols ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/hash.rb', line 22
def keys_to_symbols
z = {}
self.each do |k,v|
z[ k.is_a?(String) ? k.to_sym : k ] =
if v.class == Hash
v.keys_to_symbols
elsif v.respond_to?(:to_hash)
v.to_hash.keys_to_symbols
elsif v.respond_to?(:collect)
v.collect{|i| i.respond_to?(:keys_to_symbols) ? i.keys_to_symbols : i }
else
v.dup
end
end
z
end
|
#present? ⇒ Boolean
8
|
# File 'lib/hash.rb', line 8
def present?; ! self.empty?; end
|
do not processes recursive Arrays
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/hash.rb', line 63
def recursive_transform_values!(&block)
transform_values! do |v|
case v
when Hash
v.recursive_transform_values!( &block )
when Enumerable
v.map{|i| i.is_a?( Hash ) ? i.recursive_transform_values!( &block ) : yield( i ) }
else
yield( v )
end
end
end
|