Class: Hash
- Inherits:
-
Object
show all
- Defined in:
- lib/dev/Hash.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.get_hash_value(hash, key) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/dev/Hash.rb', line 16
def self.get_hash_value(hash,key)
return hash[key] if(hash.has_key?(key))
if key.kind_of?(String)
if key.include?(',')
keys=key.split(',')
value=get_hash_value(hash,keys.shift)
unless value.nil?
if value.kind_of?(Hash)
return Hash.get_hash_value(value,keys.join(','))
end
end
else
value=nil
ruby="value=hash[:#{key}]"
eval(ruby)
return value
end
end
nil
end
|
.print_hash(indent, hash) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/dev/Hash.rb', line 63
def self.print_hash(indent,hash)
max_length=0
hash.each { |name,value| max_length=name.to_s.length if name.to_s.length > max_length }
max_length=max_length+1
index=0
hash.each do |name,value|
prefix = "#{indent}#{name.to_s}".rjust(max_length)
if value.kind_of?(Hash)
print_hash(prefix+",",value)
elsif value.kind_of?(Array)
value.each do |v|
if v.kind_of?(Hash)
puts prefix.foreground(:yellow).bright
print_hash( prefix + " ".rjust(max_length),v)
end
puts prefix.foreground(:yellow).bright + " " + v.to_s.foreground(:green) if v.kind_of?(String)
prefix = indent + " ".rjust(max_length-1)
end
else
puts prefix.foreground(:yellow).bright + " " + value.to_s.foreground(:green)
end
index+=1
indent=" ".rjust(indent.length)
end
end
|
.set_hash_value(hash, key, value) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/dev/Hash.rb', line 37
def self.set_hash_value(hash,key,value)
if key.kind_of?(String)
if key.include?(',')
keys=key.split(',')
hval=get_hash_value(hash,keys[0])
if(hval.nil?)
ruby="hash[:#{keys[0]}]=Hash.new"
eval(ruby)
end
hval=get_hash_value(hash,keys[0])
if hval.kind_of?(Hash)
keys.shift
if keys.length == 1
Hash.set_hash_value(hval,keys[0],value)
else
Hash.set_hash_value(hval,keys.join(','),value)
end
end
else
eval("hash[:#{key}]=value")
end
else
hash[key]=value
end
end
|
Instance Method Details
#get_value(key) ⇒ Object
8
9
10
|
# File 'lib/dev/Hash.rb', line 8
def get_value(key)
return Hash.get_hash_value(self,key)
end
|
#set_value(key, value) ⇒ Object
12
13
14
|
# File 'lib/dev/Hash.rb', line 12
def set_value(key,value)
Hash.set_hash_value(self,key,value)
end
|
#strip_auto_entries ⇒ Object
remove all auto generated entries
4
5
6
|
# File 'lib/dev/Hash.rb', line 4
def strip_auto_entries
self.each { |k,v| self.delete k if !v.nil? && v.kind_of?(Hash) && v.has_key?(:auto) && v[:auto] }
end
|