NoMethodError: undefined method `yesterday 'for date: class

Every answer posted on SO (yes I checked) seems to have require 'date'

like a solution. I did this and my code still won't work.

require 'date'

yesterday = RepSched::Helpers.datestring(Date.yesterday)

      

Ruby chokes on Date.yesterday for some reason

NoMethodError: undefined method `yesterday' for Date:Class

      

What am I doing wrong?

change

Oh no! Now my problem is bigger than I thought. Now that I figured out this problem, I realize that DateTime behavior is different too!

+3


source to share


2 answers


yesterday

provided by Rails / Active Support. You can require it in your non-Rails project:

require 'active_support/core_ext/date/calculations'

Date.yesterday
#=> #<Date: 2014-09-02 ((2456903j,0s,0n),+0s,2299161j)>

      



Or calculate it yourself:

require 'date'

Date.today - 1
#=> #<Date: 2014-09-02 ((2456903j,0s,0n),+0s,2299161j)>

      

+6


source


Rails can be a little painful because it adds new methods to it Date

, which surprises many Ruby newbies. You can get yesterday

it without using Rails by running:



$ pry
[1] pry(main)> require 'date'
=> false
[2] pry(main)> Date.today
=> #<Date: 2014-09-03 ((2456904j,0s,0n),+0s,2299161j)>
[4] pry(main)> Date.today - 1
=> #<Date: 2014-09-02 ((2456903j,0s,0n),+0s,2299161j)>

      

+6


source







All Articles