Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are Rails Weekday indexes different than Ruby's?
    primarykey
    data
    text
    <p>in Rails 3.0.10 </p> <pre><code>ruby-1.9.2-p180 :010 &gt; Time::DAYS_INTO_WEEK =&gt; {:monday=&gt;0, :tuesday=&gt;1, :wednesday=&gt;2, :thursday=&gt;3, :friday=&gt;4, :saturday=&gt;5, :sunday=&gt;6} </code></pre> <p>And </p> <pre><code>ruby-1.9.2-p180 :011 &gt; Date.today =&gt; Mon, 10 Oct 2011 ruby-1.9.2-p180 :012 &gt; Date.today.wday =&gt; 1 </code></pre> <p>So, Monday is 0 in the Time mapping, and 1 in the Date mapping.<br> <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/time/calculations.rb" rel="noreferrer">https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/time/calculations.rb</a></p> <p>I feel like starting with Sunday as zero is the safer, more common, more likely to be understood by someone else way to do things. I was hoping to understand the history / reasoning in case I overlook something.</p> <p>My use case: I'm storing day of week explicitly in my db. I figured storing as an integer would be more efficient and easier to work with than storing "Monday", "Tuesday", etc.</p> <p>PS I had the great idea to do something like the following. Map back and forth from weekday strings inside methods (hiding the integer).</p> <pre><code>ruby-1.9.2-p180 :010 &gt; Time::DAYS_INTO_WEEK =&gt; {:monday=&gt;0, :tuesday=&gt;1, :wednesday=&gt;2, :thursday=&gt;3, :friday=&gt;4, :saturday=&gt;5, :sunday=&gt;6} Time::DAYS_INTO_WEEK[day_of_week.downcase.to_sym] Time::DAYS_INTO_WEEK.to_a.select{|k, v| v == start_day }.first.first.to_s.capitalize </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.
    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