Problem with using active support core extensions in my own ruby programs


#1

This code used to perform correctly with activesupport 2.0.2:

#!/usr/bin/ruby
require ‘rubygems’
require ‘time’
require ‘active_support’

class Time
include ActiveSupport::CoreExtensions::Time::Calculations
end

nxt = Time.now.change(:hour => 7)
nxt = nxt.tomorrow if nxt < Time.now
puts nxt


However, with activesupport 2.1.2 it now fails:
/var/lib/gems/1.8/gems/activesupport-2.1.2/lib/active_support/core_ext/time/calculations.rb:270:in
‘compare_without_coercion’: stack level too deep (SystemStackError)
from
/var/lib/gems/1.8/gems/activesupport-2.1.2/lib/active_support/core_ext/time/calculations.rb:270:in
‘<=>’
from ./problem_for_ruby.ml:11:in ‘<’
from ./problem_for_ruby.ml:11

The above is obviously caused by the comparison:
if nxt < Time.now

Inside calculations.rb we seem to be getting lost in:
# Layers additional behavior on Time#<=> so that DateTime and
ActiveSupport::TimeWithZone instances
# can be chronologically compared with a Time
def compare_with_coercion(other)
# if other is an ActiveSupport::TimeWithZone, coerce a Time
instance from it so we can do <=> comparision
other = other.comparable_time if
other.respond_to?(:comparable_time)
if other.acts_like?(:date)
# other is a Date/DateTime, so coerce self #to_datetime
and hand off to DateTime#<=>
to_datetime.compare_without_coercion(other)
else
compare_without_coercion(other)
end
end

compare_without_coercion is an alias:
alias_method :compare_without_coercion, :<=>

So: how to do this comparison? Is it a good idea to make use of
activesupport’s core extensions in my own ruby programs? Is this an
activesupport bug?

Many thanks,
Jonathan G.


#2

why include ActiveSupport::CoreExtensions::Time::Calculations in Time
again when active support already does it for you?


#3

On Fri, Oct 24, 2008 at 05:45:12PM +0900, Jorrel wrote:

why include ActiveSupport::CoreExtensions::Time::Calculations in Time
again when active support already does it for you?

Of course, thank you. It’s what you get when refactoring code.
(my code originally had a more limited require)

Cheers,
Jonathan.