user3292534
user3292534

Reputation:

Date returns non-existing day

I'd like to iterate over the days in month, so:

require 'time'
Time.now.month # => 5
Time.now.day.next # => 32

What does this mean? 32nd of May?

Also,

rota.rb:2:in `<main>': undefined method `days_in_month' for Time:Class (NoMethodError)

What's wrong?

Upvotes: 1

Views: 108

Answers (2)

Casper
Casper

Reputation: 34308

This should explain it:

Time.now.day.class
=> Fixnum

Fixnum doesn't know anything about dates, but it does have a method next.

If you want to advance to the next day then:

(Time.now + (60 * 60 * 24)).day
=> 1

Or if you have Rails installed you can do:

require 'active_support/time'
(Time.now + 1.day).day
=> 1

Upvotes: 1

frostmatthew
frostmatthew

Reputation: 3298

The next call has nothing to do with date or time - Time.now.day returns an integer. Calling next (or succ) returns that number plus one. See Fixnum documentation for more info.

Upvotes: 1

Related Questions