Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First and foremost don't use upper-case letters for variables in Ruby as you did with <code>SomeHash</code> and <code>XPath</code>. When a variable name starts with an upper-case letter, it means it's a constant and you probably didn't want it to be a constant.</p> <p><code>each</code> is not the best way to do this, you can do this much more simply with <code>inject</code> as in:</p> <pre><code>countries = { "Albania"=&gt;"Europe", "Andorra"=&gt;"Europe", "Austria"=&gt;"Europe", "Lebanon"=&gt;"Asia", "Macau"=&gt;"Asia", "Malaysia"=&gt;"Asia", "Papua New Guinea"=&gt;"Asia", "Jamaica"=&gt;"North America", "Martinique"=&gt;"North America", "Argentina"=&gt;"South America", "Chile"=&gt;"South America", "Sao Tome and Principe"=&gt;"Africa", "Senegal"=&gt;"Africa", "Somalia"=&gt;"Africa"} by_continents = countries.inject({}) do |memo, (k,v)| memo[v] ||= [] memo[v] &lt;&lt; k memo end </code></pre> <p>The output for this is:</p> <pre><code>{"Europe"=&gt;["Albania", "Andorra", "Austria"], "Asia"=&gt;["Lebanon", "Macau", "Malaysia", "Papua New Guinea"], "North America"=&gt;["Jamaica", "Martinique"], "South America"=&gt;["Argentina", "Chile"], "Africa"=&gt;["Sao Tome and Principe", "Senegal", "Somalia"]} </code></pre> <p>You have all countries grouped by continent and you can pick any one of them.</p> <p>In your code it should be placed like this:</p> <pre><code>def country inflation_hash = {} XPath.match( data, "//country").map do |element| inflation_hash[element.attributes["name"]] = element.attributes["continent"] end by_continents = inflation_hash.inject({}) do |memo, (k,v)| memo[v] ||= [] memo[v] &lt;&lt; k memo end puts by_continents.inspect by_continents end </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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