Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your method is returning the result of <code>task_names.each</code>. <code>each</code> always returns what it started with. So you need to actually return your result.</p> <p>Also, you are recreating you <code>to_do_this_week</code> array on every iteration of the loop, which would wipe it clean.</p> <pre><code>def tasks_for_week(week, *task_names) to_do_this_week = [] task_names.each do |task_name| if some_condition to_do_this_week &lt;&lt; task_name end end to_do_this_week end </code></pre> <p>Or this:</p> <pre><code>def tasks_for_week(week, *task_names) returning [] do |to_do_this_week| task_names.each do |task_name| if some_condition to_do_this_week &lt;&lt; task_name end end end end </code></pre> <p>But I think this is probably your best best:</p> <pre><code>def tasks_for_week(week, *task_names) task_names.find_all do |task_name| some_condition end end </code></pre> <p>That last one uses <code>find_all</code> which iterates over an array and will return a new array populated with any objects that the block return a true value for.</p> <p>Lastly, your conditional logic is a bit crazy too. You can use the <code>[]</code> accessors for active record fields in a dynamic way. And it's usually clearer to use the positive case instead of the double negative of <code>unless something.nil?</code>. And if this is a common use for creating a range, it might be best to farm that out to a method:</p> <pre><code>def week_range_for_task(task) self["#{task_name}_week_min"]..self["#{task_name}_week_max"] end ... self[task_name] &amp;&amp; week_range_for_task(task_name).include?(week) </code></pre> <p>Making the whole method:</p> <pre><code>def tasks_for_week(week, *task_names) task_names.find_all do |task_name| self[task_name] &amp;&amp; week_range_for_task(task_name).include?(week) end 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.
 

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