Question

I have the following written in ruby

t = Time.now
    t.strftime("%Y-%d-%m")

SCHEDULER.every '1m', :first_in => 0 do |job|
  send_event('gmail_gh', {current: gmail.inbox.count(:after => Date.parse(t)), older: gmail.inbox.count})  

But i get this error

scheduler caught exception:
can't convert Time into String
/var/dashing/cdmdash/jobs/gmail_gh.rb:21:in `parse'
/var/dashing/cdmdash/jobs/gmail_gh.rb:21:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/jobs.rb:231:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/jobs.rb:231:in `trigger_block'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/jobs.rb:191:in `block in trigger'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/scheduler.rb:416:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/scheduler.rb:416:in `block in trigger_job'

I think it has something to do with the t variable and it not being a truing, I am new to Ruby so I am abit stuck

Was it helpful?

Solution

If you look at the gem documentation, you will see that the :after and :before params take in a date in the format of YYYY-MM-DD.

From the gem Readme:

gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
gmail.inbox.count(:on => Date.parse("2010-04-15"))

Your code is passing in YYYY-DD-MM which is likely causing the error.

Edit

When you call strftime on a datetime object, it doesn't modify the object - only returns the string notation based on format you give.

As a result, the Date.parse(t) is still getting Time.now was a parameter.

Try this:

t = Time.now.strftime("%Y-%m-%d")
Date.parse(t)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top