5

Every answer posted on SO (yes, I checked) seems to have require 'date' as a solution. I've done that and my code still won't work.

require 'date' yesterday = RepSched::Helpers.datestring(Date.yesterday) 

For some reason, Ruby chokes on Date.yesterday

NoMethodError: undefined method `yesterday' for Date:Class 

What am I doing wrong?

edit

Oh no! Now my problem is bigger than I thought. Now that I've realized that issue, I realize DateTime's behavior is different too!

6
  • 4
    yesterday is a method added by Rails Commented Sep 3, 2014 at 14:56
  • 1
    You sure you're running under Rails? Commented Sep 3, 2014 at 14:56
  • Oh. Sinatra. Hah, woops! Commented Sep 3, 2014 at 14:57
  • Try require 'active_support/core_ext/date/calculations' Commented Sep 3, 2014 at 14:59
  • 1
    @sent1nel Don't do that if you're not running under Rails :) Commented Sep 3, 2014 at 15:04

2 Answers 2

9

yesterday is provided by Rails / Active Support. You can either 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)> 
Sign up to request clarification or add additional context in comments.

Comments

6

Rails can be a bit of a pain because it adds new methods to Date which surprises many newcomers to Ruby. You can get yesterday, without using Rails by doing:

$ 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)> 

1 Comment

I've chosen this answer because it better describes my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.