Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's look at the code in question:</p> <pre><code>override.reduce({}) { |hsh,(k,v)| hsh.merge(k.to_s =&gt; v) } </code></pre> <p>Now let's look at the <a href="http://ruby-doc.org/core-2.0/Enumerable.html#method-i-reduce" rel="nofollow">docs for <code>Enumerable#reduce</code></a>:</p> <blockquote> <p>Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.</p> <p>If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.</p> </blockquote> <p>So, override is going to be your typical Ruby options hash, like:</p> <pre><code>{ debug: 'true', awesomeness: 'maximum' } </code></pre> <p>So what happens when you use that <code>reduce</code> on override?</p> <p>It will combine all the elements of the enum (key => value pairs of the override hash) using the binary function <code>merge</code>. Merge takes a hash and merges it into the receiver. So what's happening here?</p> <ol> <li><code>hsh</code> starts out as <code>{}</code> and the first key/value pair is merged: <code>{}.merge(:debug.to_s =&gt; "true")</code>.</li> <li><code>hsh</code> is now <code>{"debug" =&gt; "true"}</code>.</li> <li>The next key/value pair is merged into that: <code>{"debug" =&gt; "true"}.merge(:awesomeness.to_s =&gt; "maximum")</code>.</li> <li><code>hsh</code> is now <code>{"debug" =&gt; "true", "awesomeness" =&gt; "maximum"}</code></li> <li>There are no more elements, so this value of <code>hsh</code> is returned.</li> </ol> <p>This matches up with the code comment, which says "Convert any symbol keys to strings and remove the old key/values", although technically the old values are not removed. Rather, a new hash is constructed and the old hash with the old values is discarded by replacing the variable with the new value, to eventually be collected – along with the intermediate objects created by the merges in the reduce – by the garbage collector. As an aside, this means that <code>merge!</code> would be slightly more efficient than <code>merge</code> in this case as it would not create those intermediate objects.</p> <hr> <p><code>__foo__</code> is a ruby idiom for a quasi-private and/or 'core' method that you want to make sure isn't redefined, e.g., <code>__send__</code> because things like <code>Socket</code> want to use <code>send</code>. In Ruby, <code>hash</code> is the hash value of an object (computed using a hash function, used when the object is used as a hash key), so <code>__hash__</code> probably points to an instance variable of the <code>options</code> object that stores its data as a hash. <a href="http://ruby-doc.org/gems/docs/n/nested_config-0.3.0/NestedConfig.html" rel="nofollow">Here's a class from a gem that does just that</a>. You'd have to look at the docs for whatever type of object <code>options</code> is to be sure though. (You'd have to look at the code to be <em>really</em> sure. ;)</p>
 

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