Note that there are some explanatory texts on larger screens.

plurals
  1. POImprove this recursive function for hash traversal in Ruby
    primarykey
    data
    text
    <p>I've written a method to turn a hash (nested if necessary) of values into a chain that can be used with eval to dynamically return values from an object.</p> <p>E.g. passed a hash like { :user => { :club => :title }}, it will return "user.club.title", which I can then eval. (The point of this is to write a method for views that will allow me to dump the contents of objects rapidly, by passing in the object and a list of attributes that I want to display, e.g.: item_row(@user, :name, :email, { :club => :title })</p> <p>Here's what I've got. It works, but I know it can be improved. Curious to see how you'd improve it.</p> <pre><code># hash = { :user =&gt; { :club =&gt; :title }} # we want to end up with user.club.title def hash_to_eval_chain(hash) raise "Hash cannot contain multiple key-value pairs unless they are nested" if hash.size &gt; 1 hash.each_pair do |key, value| chain = key.to_s + "." if value.is_a? String or value.is_a? Symbol chain += value.to_s elsif value.is_a? Hash chain += hash_to_eval_chain(value) else raise "Only strings, symbols, and hashes are allowed as values in the hash." end # returning from inside the each_pair block only makes sense because we only ever accept hashes # with a single key-value pair return chain end end puts hash_to_eval_chain({ :club =&gt; :title }) # =&gt; club.title puts hash_to_eval_chain({ :user =&gt; { :club =&gt; :title }}) # =&gt; user.club.title puts hash_to_eval_chain({ :user =&gt; { :club =&gt; { :owners =&gt; :name }}}) # =&gt; user.club.owners.name puts ({ :user =&gt; { :club =&gt; { :owners =&gt; :name }}}).to_s # =&gt; userclubownersname (close, but lacks the periods) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload