Please Contribute

  • Thread starter Bhardwaj, Aadarsh
  • Start date
B

Bhardwaj, Aadarsh

Dear all
Please contribute in enhancing the functionality of my website -
please visit - http://techie.bewhizkid.com

I need your valuable comments, if any one wants to contribute please
let me know.


2010/2/28 Glenn Ritz said:
Robert said:
2010/2/24 Xavier Noria <[email protected]>:

{'en' =3D> {'A' =3D> Fixnum, 'B' =3D> {'C' =3D> Fixnum, 'D' =3D> Stri= ng }}
=EF=BF=BD =EF=BF=BDnewh[k] =3D v.is_a?(Hash) ? classify_values(v) : v.= class
=EF=BF=BDend
=EF=BF=BDnewh
end

p classify_values({'en' =3D> {'A' =3D> 1, 'B' =3D> {'C' =3D> 3, 'D' = =3D> 'four'
}}})

I would do it a tad differently:

def classify(o)
case o
when Hash
h =3D {}
o.each {|k,v| h[k] =3D classify(v)}
h
else
o.class
end
end

Advantage is that you can stuff anything in the method while your
variant requires the argument to be a Hash. The difference may seem
subtle but if you add more collection types for special treatment,
you'll will notice a difference in effort to implement it. I can
simply do

def classify(o)
case o
when Hash
h =3D {}
o.each {|k,v| h[k] =3D classify(v)}
h
when Array
o.map {|v| classify(v)}
else
o.class
end
end

while you need to do a more complicated code change.

Kind regards

robert

Thanks, this is very helpful.

I am trying to adapt this a bit, but with no luck so far.

What I'd like to do is to start with a hash and have the above code be
additive.

In other words, if I have the following code:

h0 =3D {}
h1 =3D {'en' =3D> {'A' =3D> 5, 'B' =3D> { 'C' =3D> 'xxx', 'D' =3D> { 'E'= =3D> 4 }}}}
h2 =3D {'en' =3D> {'F' =3D> 'yyy'}}


I'd like to be able to call the classify method like this:

puts h0.classify(h1).classify(h2).inspect

And get the following result:

{'en' =3D> {'A' =3D> Fixnum, 'B' =3D> { 'C' =3D> String, 'D' =3D> { 'E' = =3D> Fixnum
}}, 'F' =3D> String }}

So I figured it would be something like this:

class Hash
def classify(o)
case o
when Hash
h =3D self
o.each {|k,v| h[k] =3D classify(v)}
h
when Array
o.map {|v| classify(v)}
else
o.class
end
end
end

But this is probably wrong in a few ways. Any suggestions would be
appreciated.

I would separate this in two steps:

1. merge Hashes intelligently (i.e. with a bit more logic than
Hash#merge default behavior).

2. classify.

So this would probably be something like

class Hash
def merge_deep!(hs)
merge! hs do |key,old_val,new_val|
case old_val
when Hash
old_val.merge_deep! new_val
else
new_val
end
end
end
end

see
http://www.ruby-doc.org/ruby-1.9/classes/Hash.html#M000406

Kind regards

robert


--=20
With Regards,

Adarsh Bhardwaj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top