Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Can't extend Date in Rails 3 with Ruby 1.9.2-p0

#5726

In my Rails 3 app I was able to add a file in lib called date.rb with the following contents:

class Date
  def id
    self.to_s
  end
  
  def self.find(id)
    Date.parse(id)
  end
end

This worked in 1.8.7-p174, however, when doing the upgrade to Rails 1.9.2-p0, it's been failing. When I try to run my tests I get this:

/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:91:in `alias_method': undefined method `+' for class `Date' (NameError)
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:91:in `<class:Date>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:7:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/ruby/shim.rb:12:in `require'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/ruby/shim.rb:12:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller.rb:6:in `require'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller.rb:6:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_controller.rb:1:in `require'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_controller.rb:1:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_controller/railtie.rb:2:in `require'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_controller/railtie.rb:2:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/railtie.rb:9:in `require'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/railtie.rb:9:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/all.rb:11:in `require'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/all.rb:11:in `block in <top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/all.rb:5:in `each'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/all.rb:5:in `<top (required)>'
    from /Users/avand/Git/foo/config/application.rb:3:in `require'
    from /Users/avand/Git/foo/config/application.rb:3:in `<top (required)>'
    from /Users/avand/Git/foo/config/environment.rb:2:in `require'
    from /Users/avand/Git/foo/config/environment.rb:2:in `<top (required)>'
    from /Users/avand/Git/foo/test/test_helper.rb:2:in `require'
    from /Users/avand/Git/foo/test/test_helper.rb:2:in `<top (required)>'
    from test/unit/user_test.rb:1:in `require'
    from test/unit/user_test.rb:1:in `<top (required)>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `block in <main>'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
    from /Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `<main>'

I tried commenting the two methods out (leaving the class declaration) to no avail. It seems that simply trying to extend Date causes some conflict within ActiveSupport's core extensions.

Going to try working around this issue for now.

Reported by Avand · September 28th, 2010 @ 07:58 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Avand
    Avand

    Amazingly, if I change the file name to date_extensions.rb it works. Odd.

    September 28th, 2010 @ 08:23 PM

  2. Henrik Hodne
    Henrik Hodne

    Could you please put this in the top of the file and paste the extra line it prints? (should begin with "LOAD PATH: ")

    puts "LOAD PATH: #{$LOAD_PATH.inspect}"
    

    I think that since this file would be before the ruby core date.rb, it requires your file instead or something.

    September 29th, 2010 @ 06:03 AM

  3. Avand
    Avand

    Not the easiest thing to paste, but here it is:

    LOAD PATH: ["/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/haml-3.0.18/vendor/fssm/lib", "/Users/avand/Git/sqoot/lib", "/Users/avand/Git/sqoot/vendor", "/Users/avand/Git/sqoot/app/controllers", "/Users/avand/Git/sqoot/app/helpers", "/Users/avand/Git/sqoot/app/javascripts", "/Users/avand/Git/sqoot/app/mailers", "/Users/avand/Git/sqoot/app/models", "/Users/avand/Git/sqoot/app/stylesheets", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/bitly-0.5.3/lib/bitly", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/bitly-0.5.3/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/aws-s3-0.6.2/lib/aws", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/twitter-0.9.12/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rturk-2.3.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rest-client-1.6.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rails-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/thor-0.14.2/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.9.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.9.0/ext", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/oauth-0.4.3/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/nokogiri-1.4.3.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/multi_json-0.0.4/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/maruku-0.6.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/syntax-1.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/json-1.4.6/ext/json/ext", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/json-1.4.6/ext", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/json-1.4.6/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/jsmin-1.0.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/hoptoad_notifier-2.3.8/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/hashie-0.4.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/haml-3.0.18/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/garb-0.8.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/friendly_id-3.1.7/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/delayed_job-2.1.0.pre2/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.6.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/crack-0.1.8/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/babosa-0.2.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/aws-s3-0.6.2/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/xml-simple-1.0.12/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/ancestry-1.2.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/acts-as-taggable-on-2.0.6/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activeresource-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/mail-2.2.6.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/treetop-1.4.8/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/polyglot-0.3.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/mime-types-1.16/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/tzinfo-0.3.23/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.6/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/erubis-2.6.6/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/i18n-0.4.1/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/abstract-1.0.0/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.4.0", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.4.0", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1", "/Users/avand/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.4.0", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_controller/vendor/html-scanner", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/vendor/multimap", "/Users/avand/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/vendor/regin"]
    

    October 1st, 2010 @ 06:02 AM

  4. Benoit Daloze
    Benoit Daloze

    @Henrik I would rather ask $LOADED_FEATURES or $"

    Could you please add this in the top of your file and paste the result between BEGIN and END ?

    puts "BEGIN"; puts $"; puts "END"
    

    October 2nd, 2010 @ 08:13 PM

  5. Henrik Hodne
    Henrik Hodne
    • Tag changed from 3.0.0, date, ruby-1.9.2 to load path, 3.0.0, date, ruby-1.9.2

    @Benoit Oops, yes. Dunno why I asked for the load path.

    Anyways, Avand, I've done some testing and if there's a file in your load path called date.rb, Ruby will load that instead of it's internal one for Date for some odd reason. So I would suggest renaming it to something else (like date_extensions.rb).

    October 3rd, 2010 @ 01:44 AM

  6. Benoit Daloze
    Benoit Daloze

    @Henrik Eventually, both are be useful :)

    The reason is simple: the lib/ folder is before the stdlib in $LOAD_PATH (lib is 2nd, stdlib is 71st).

    The source of the problem is then the lib/ folder to be #unshift to $LOAD_PATH.
    Do you know why Rails do this?

    This let you override the stdlib libraries, but also prevent you for ever loading them, which does not seems right (except if those were loaded before $: was changed, which should be the case as AS use Date, but this might be a problem of #autoload).

    If stdlib was before, you would be forced to load your file with an absolute path, which I think make sense when you use a file with the same name.

    @Avand: Rather than renaming it, I would put it in a subfolder of lib/, like "extensions", and so do a

    require 'extensions/date'
    

    which seems explicit and avoid conflicts.

    October 3rd, 2010 @ 12:11 PM

  7. Avand
    Avand

    Either of those solutions will work for my purposes. Thanks @Henrik and @Benoit.

    At a higher level though, it would seem that what I did first, shouldn't have bombed like it did - I would have expected it to just work and re-open the class as defined by Rails.

    October 4th, 2010 @ 09:09 PM

  8. Ryan Bigg
    Ryan Bigg
    • State changed from new to resolved
    • Importance changed from to Low

    When you call a file "date" and place it in the lib directory and attempt to require the ruby +'date'+, you'll get this one instead. It may not be you who's requiring it, but still: best practice is to NOT call your files with the same name as what they are extending.

    An _ext.rb extension or the extensions directory like Benoit suggested are the best practices.

    October 5th, 2010 @ 03:43 AM