T
Trans
Having a little trouble here. seems that I'm getting some errors trying
to dup certain values (like FIXNUM), or if I try clone it says that I
can't modify a frozen string. Is there a way to achieve this
functionality? Here's the code:
class Hash
# Returns a new hash created by traversing the hash and its
# subhashes, executing the given block on each key/value pair.
#
# h = { "A"=>"A", "B"=>"B" }
# h = h.traverse { |k,v| k.downcase! }
# h #=> { "a"=>"A", "b"=>"B" }
#
def traverse( &yld )
h = {}
self.each_pair do |k,v|
q = k.dup
f = v.dup
if f.kind_of?(Hash)
h[q] = f.traverse( &yld )
else
yield(q, f)
h[q] = f
end
end
return h
end
end
Thanks,
T.
to dup certain values (like FIXNUM), or if I try clone it says that I
can't modify a frozen string. Is there a way to achieve this
functionality? Here's the code:
class Hash
# Returns a new hash created by traversing the hash and its
# subhashes, executing the given block on each key/value pair.
#
# h = { "A"=>"A", "B"=>"B" }
# h = h.traverse { |k,v| k.downcase! }
# h #=> { "a"=>"A", "b"=>"B" }
#
def traverse( &yld )
h = {}
self.each_pair do |k,v|
q = k.dup
f = v.dup
if f.kind_of?(Hash)
h[q] = f.traverse( &yld )
else
yield(q, f)
h[q] = f
end
end
return h
end
end
Thanks,
T.