RubyFlow : The Ruby Community Blog

Home   Submit   Sign Up   Log In   leaders   Twitter   RSS Feed  
 

Recursively Setting Deep Hash Value

I’ve always wanted to be able to do something like:
my_hash = {}
my_hash["Cloud"]["Stats"]["Strength"] = 100
p my_hash # => {"Cloud"=>{"Stats"=>{"Strength"=>100}}}

So I whipped up this small code and now I can! Metaprogramming wins again!

Comments

Alternatively, Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }Sutto - May 17, 2011 07:32
@sutto
your version does'nt work with ruby 1.8.7 (at least not for me).
but 1.9.2 works fine.
nice solution, both of you.collin - May 17, 2011 09:20
Both solutions are nice. But I was wondering if you are familiar with ruby facets. It's a very cool Ruby library and has a solution for this problem. You can take a look to http://rubyworks.github.com/facets/doc/api/core/Hash.html for the autonew method.lucapette - May 17, 2011 17:04
didn't you already post it, http://www.rubyflow.com/items/5667-recursively-setting-deep-hash-value

christ man, get a life and stop wasting my timeangrynerd - May 21, 2011 11:26
http://devguru.pl/post/6032714814/recursively-setting-deep-hash-value-revisitediktorn - May 31, 2011 09:30
How to prolong inspiron 1525 batteryinspiron 1525 battery's life?Well,the dv6000 battery is lithium-ion battery.It works on ion movement between the positive and negative electrodes.The easiest way to give your t61 battery an early death is to damage it.And the two most common causes of damage inspiron 6400 battery are from overheating and using an AC adapter with the wrong voltage.Use a cooling pad when using hp g60 battery.Clean your desk,dust will get into the vents and clog the cooling fan making hp dv7 battery have worse life.Consider taking your pa3399u battery out when using your laptop plugged into AC power.You do not need to discharge g4 powerbook battery fully and recharge constantly.For a new dell d800 battery ,you're not supposed to store it empty.And t60 battery left out of the machine will lose charge over time. You're not supposed to leave the dell d620 battery in the machine and run on AC all the time. Doing so will kill the life expectancy.You're not supposed to charge cycle the dell d630 battery frequently, as the long-term life expectancy is measured in charge cycles.
You're not supposed to deep discharge the nv52 battery.You're not supposed to use the dell inspiron 6000 battery before it has finished charging.Well,my question is two-fold,Which of these are true?how to maximizing the long-term life of a pa3534u battery.I'm fairly sure that (c) is true, as I have been a lot more careful about charge-cycling than I was with my previous m1210 battery.This dv2000 battery is still at about 90% of the original capacity after a year.
I usually run my laptop with the inspiron 1545 battery.My advice is to avoid using the inspiron 1501 battery for just a few minutes.If you use the sony bps13 battery,drain the a1185 battery, then recharge once it gets down below 5-7%.Otherwise, keep it on AC power, with or without the inspiron 1520 battery inside. This has worked well for me.If u this this is good advice for extending your hpdv4 battery life,just do it like I said.Then u can own long life laptops batteries.laptops batteries - September 03, 2011 08:26

Post a Comment

Note: If you are a registered user, you can log in to populate these fields.

You may use ONLY these HTML tags to format your comment:

<a href="" title=""> <b> <blockquote> <code> <em> <i> <strong>

Do NOT use <p> tags. Just use newlines :-)

t
o
y
a
m
l
U tits
e
q
l