Opening the Time Class

I’m trying to add some extra methods to the time class by opening it
up like this:

class Time # opening up ruby time


def self.yesterday_at_midnight(today =
time_yesterday = today.to_i - ONE_DAY_IN_SECS
yesterday = time_yesterday )
return "#{yesterday.year}-#{yesterday.month_with_leading_zero}-
#{yesterday.day_with_leading_zero} "

… etc


When I run my test from within NetBeans 6.0.1…

require ‘test/unit’

class TimeTest < Test::Unit::TestCase

MONDAY_05_05_2008 = 1209972973

def test_can_open_time_to_add_yesterday_at_midnight
today = Time.yesterday_at_midnight(
expected = “2008-05-04”
assert( expected == today)


This works and I get a passing test.

However if I include an additional require line in my test: require
‘unit_test_helper’ then I get the following error:

load_specification': undefined methodparse’ for Time:Class
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb:
314:in date=' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb: 416:insend’
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb:
416:in initialize' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb: 412:ineach’
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb:
412:in initialize' from (eval):1:innew’
from (eval):1:in load_specification' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/source_index.rb: 128:ineval’
… 11 levels…
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:211:in
synchronize' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:211:insearcher’
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
30:in `require’
from C:/dev/esp/test/unit/models/time_test.rb:2

I think this has something to do with not requiring time properly, but
I can’t work it out. Has anyone has a similar problem?


I managed to solve my problem using the Chronic library.

irb(main):002:0> require ‘chronic’
=> true
irb(main):003:0> Chronic.parse(‘yesterday’)
=> Tue May 06 12:00:00 +1000 2008

Um… = This time yesterday

Therefore, = Midnight, yesterday.

yep, your logic is sound.

On sqlite3, doing a select with the date ‘2008-05-07’ defaults to

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs