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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jonathan G. (Guest)
on 2008-10-24 12:42
(Received via mailing list)
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.
Jorrel (Guest)
on 2008-10-24 12:48
(Received via mailing list)
why include ActiveSupport::CoreExtensions::Time::Calculations in Time
again when active support already does it for you?
Jonathan G. (Guest)
on 2008-10-24 12:55
(Received via mailing list)
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.
This topic is locked and can not be replied to.