[ruby-talk:442998] Using activesupport outside rails

I have stripped some code out of a Rails app to run as a plain ruby app
that used Time.zone... that is supplied by activesupport. I'm getting the
error "undefined method `yaml_as' for BigDecimal:Class (NoMethodError)"
when I require activesupport and try to run it

The solutions on the interwebs is "just install Rails". I really do not
want to drag all that baggage for what is only one line of code in my
application. Is there a way of patching the issue (yaml_as in BigDecimal)?

Can you share more of the code that's causing the problem?

$ bundle list
Gems included by the bundle:
  * activesupport (7.0.4)
  * concurrent-ruby (1.1.10)
  * i18n (1.12.0)
  * minitest (5.16.3)
  * tzinfo (2.0.5)
Use `bundle info` to print more detailed information about a gem

$ cat foo.rb
require 'active_support'
require 'active_support/core_ext/time/zones'

Time.zone = 'UTC'
p Time.zone.now
Time.zone = 'America/New_York'
p Time.zone.now

$ bundle exec ruby foo.rb
Thu, 22 Sep 2022 11:59:05.012889055 UTC +00:00
Thu, 22 Sep 2022 07:59:05.015970309 EDT -04:00

···

On 9/22/22, Peter Hickman <peterhickman386@googlemail.com> wrote:

I have stripped some code out of a Rails app to run as a plain ruby app
that used Time.zone... that is supplied by activesupport. I'm getting the
error "undefined method `yaml_as' for BigDecimal:Class (NoMethodError)"
when I require activesupport and try to run it

$ bundle list
Gems included by the bundle:
  * activesupport (6.1.7)
  * concurrent-ruby (1.1.10)
  * i18n (1.12.0)
  * iconv (1.0.8)
  * minitest (5.16.3)
  * multi_json (1.15.0)
  * mustermann (1.1.1)
  * nokogiri (1.13.4)
  * pg (1.3.5)
  * racc (1.6.0)
  * rack (2.2.3)
  * rack-protection (2.2.0)
  * ruby2_keywords (0.0.5)
  * sequel (5.55.0)
  * sinatra (2.2.0)
  * sinatra-contrib (2.2.0)
  * tilt (2.0.10)
  * tzinfo (2.0.5)
  * webrick (1.7.0)
  * zeitwerk (2.6.0)

Your foo.rb script runs fine
$ bundle exec ruby ./foo.rb
Thu, 22 Sep 2022 12:25:18.261950000 UTC +00:00
Thu, 22 Sep 2022 08:25:18.263452000 EDT -04:00

However in my app runs I get

E, [2022-09-22T13:26:28.361967 #50326] ERROR -- : ERROR: undefined method
`zone' for Time:Class

from the line
Time.zone.at((Time.zone.now.to_f / 300).floor * 300)

The script has this at the top

require 'iconv'
require 'active_support'

Try:
require 'active_support/core_ext/time/zones'

$ bundle list
Gems included by the bundle:
  * activesupport (7.0.4)
  * concurrent-ruby (1.1.10)
  * i18n (1.12.0)
  * iconv (1.0.8)
  * minitest (5.16.3)
  * tzinfo (2.0.5)
Use `bundle info` to print more detailed information about a gem

$ cat foo.rb
require 'iconv'
require 'active_support'
require 'active_support/core_ext/time/zones'

Time.zone = 'America/New_York'
p Time.zone.at((Time.zone.now.to_f / 300).floor * 300)

$ bundle exec ruby foo.rb
Thu, 22 Sep 2022 08:40:00.000000000 EDT -04:00

···

On 9/22/22, Peter Hickman <peterhickman386@googlemail.com> wrote:

E, [2022-09-22T13:26:28.361967 #50326] ERROR -- : ERROR: undefined method
`zone' for Time:Class
from the line
Time.zone.at((Time.zone.now.to_f / 300).floor * 300)

The script has this at the top
require 'iconv'
require 'active_support'

I have stripped some code out of a Rails app to run as a plain ruby app

that used Time.zone... that is supplied by activesupport. I'm getting the
error "undefined method `yaml_as' for BigDecimal:Class (NoMethodError)"
when I require activesupport and try to run it

This seems to be a bug, AS should be self-contained and if you load part of
it, dependencies should be required as needed.

Could you please open an issue in the Rails repository and tag me please?

···

On Thu, Sep 22, 2022 at 1:33 PM Peter Hickman < peterhickman386@googlemail.com> wrote:

Ah I missed that, new error "ERROR: undefined method `now' for
nil:NilClass"

···

On Thu, 22 Sept 2022 at 13:41, Frank J. Cameron <fjc@fastmail.net> wrote:

Try:
require 'active_support/core_ext/time/zones'

Might have a clue here, I thought that Time.zone was being set to a default
value. If I force it then I am not seeing errors. Got to find out where the
original app set it from

activesupport-7.0.4/lib/active_support/railtie.rb:

    # Sets the default value for Time.zone
    # If assigned value cannot be matched to a TimeZone, an exception
will be raised.
    initializer "active_support.initialize_time_zone" do |app|
      begin
        TZInfo::DataSource.get
      rescue TZInfo::DataSourceNotFound => e
        raise e.exception "tzinfo-data is not present. Please add gem
'tzinfo-data' to your Gemfile and run bundle install"
      end
      require "active_support/core_ext/time/zones"
      Time.zone_default = Time.find_zone!(app.config.time_zone)
    end

config/application.rb:

    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.

···

On 9/22/22, Peter Hickman <peterhickman386@googlemail.com> wrote:

Might have a clue here, I thought that Time.zone was being set to a default
value. If I force it then I am not seeing errors. Got to find out where the
original app set it from

    #
    # config.time_zone = "Central Time (US & Canada)"

Thanks for the info. The original app has 'Europe/London' set so I can use
that. I should probably look for a more robust solution but this gets
things working

This is just phase 1, the original codebase being split up and stripped
down. The next stage will rewrite the code from scratch where it can be
better addressed

Thank you for your help

···

On Thu, 22 Sept 2022 at 15:07, Frank J. Cameron <fjc@fastmail.net> wrote:

On 9/22/22, Peter Hickman <peterhickman386@googlemail.com> wrote:
> Might have a clue here, I thought that Time.zone was being set to a
default
> value. If I force it then I am not seeing errors. Got to find out where
the
> original app set it from

activesupport-7.0.4/lib/active_support/railtie.rb:

    # Sets the default value for Time.zone
    # If assigned value cannot be matched to a TimeZone, an exception
will be raised.
    initializer "active_support.initialize_time_zone" do |app|
      begin
        TZInfo::DataSource.get
      rescue TZInfo::DataSourceNotFound => e
        raise e.exception "tzinfo-data is not present. Please add gem
'tzinfo-data' to your Gemfile and run bundle install"
      end
      require "active_support/core_ext/time/zones"
      Time.zone_default = Time.find_zone!(app.config.time_zone)
    end

config/application.rb:

    # These settings can be overridden in specific environments using the
files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;