Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This logic is incorrect:</p> <pre><code>time.each do |time| @array = Array(DateTime.strptime(time,'%m/%d/%Y %H:%M').hour) end </code></pre> <p>The body of the loop is <em>replacing</em> the contents of <code>@array</code> every time, so you only get the last element when you're done. Also, using <code>time</code> as the inner variable name is confusing (and destructive in older versions of Ruby). </p> <p>What you want is to <em>append</em> to the array with <code>&lt;&lt;</code> inside the loop, instead of <em>assigning</em> to it with <code>=</code>:</p> <pre><code>@array = [] time.each do |t| @array &lt;&lt; DateTime.strptime(t, '%m/%d/%Y %H:%M').hour end </code></pre> <p>But that's a very procedural way of building an array, and not very Rubyish. The idiomatic way would be to use <code>map</code> to construct the new array all at once:</p> <pre><code>@array = time.map { |t| DateTime.strptime(t, '%m/%d/%Y %H:%M').hour } </code></pre> <p>As a side note, I'm not sure why you decided to make <code>@array</code> an instance variable. You could just call it <code>array</code>; using <code>@</code> for arrays is a Perl thing, not Ruby.</p> <p>Anyway, once you fix the creation of <code>@array</code>, your logic for building the count Hash should work as-is. You could, however, build it in a similar way by using <code>reduce</code>; this is just one possibility:</p> <pre><code>result = @array.reduce({}) { |h, t| h.merge({t =&gt; h[t]+1}) } </code></pre> <p>You can further simplify the logic by using a built-in method of Ruby arrays called <code>group_by</code>. This call:</p> <pre><code>time.group_by { |t| DateTime.strptime(t, '%m/%d/%Y %H:%M').hour } </code></pre> <p>returns this Hash:</p> <pre><code>{10=&gt;["11/12/08 10:47"], 13=&gt;["11/12/08 13:23", "11/12/08 13:30"], 14=&gt;["11/12/08 14:04", "11/12/08 14:46"]} </code></pre> <p>That's close to what you want in <code>result</code>; all you have to do is replace those array values with their lengths. Fortunately, <code>map</code> works on Hashes, too, but what it returns is an array of arrays instead of another Hash, so you have to convert it back when you're done. This will do the trick:</p> <pre><code>result = Hash[time.group_by { |t| DateTime.strptime(t, '%m/%d/%Y %H:%M').hour }.map{|k,v| [k, v.length]}] </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