Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/dev/Hash.rb
Direct Known Subclasses
Class Method Summary collapse
- .get_hash_value(hash, key) ⇒ Object
- .print_hash(indent, hash) ⇒ Object
- .set_hash_value(hash, key, value) ⇒ Object
Instance Method Summary collapse
Class Method Details
.get_hash_value(hash, key) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dev/Hash.rb', line 11 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dev/Hash.rb', line 58 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dev/Hash.rb', line 32 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
3 4 5 |
# File 'lib/dev/Hash.rb', line 3 def get_value(key) return Hash.get_hash_value(self,key) end |
#set_value(key, value) ⇒ Object
7 8 9 |
# File 'lib/dev/Hash.rb', line 7 def set_value(key,value) Hash.set_hash_value(self,key,value) end |