Forum: Ruby on Rails Using a module within another module

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.
Juan M. (Guest)
on 2007-07-08 05:16
Hi,

I'm trying to use the module
ActiveSupport::CoreExtensions::Time::Calculations in a helper module,
but I get a nasty error. My module looks like this:

require 'rubygems'
gem 'activesupport'

include ActiveSupport::CoreExtensions::Time::Calculations

module CalendarHelper

# code....

days = self.class.days_in_month(month, year)

# more code...

But every time I invoke it, I get the following error:

#<NameError: cannot remove Object::ClassMethods>
["/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:422:in
`remove_const'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:422:in
`remove_constant'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:273:in
`remove_unloadable_constants!'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:273:in
`remove_unloadable_constants!'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:72:in
`clear'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:60:in
`reset_application!'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:116:in
`reset_after_dispatch'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:51:in
`dispatch'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in
`handle_dispatch'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in
`service'", "/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in
`service'", "/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'",
"/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'",
"/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'",
"/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'",
"/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'",
"/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'",
"/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in
`dispatch'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/webrick.rb:59",
"/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in
`require'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in
`new_constants_in'",
"/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in
`require'",
"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39",
"/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'", "script/server:3"]

Do you know what's going on ? It's a bit hard to be a newbie in this RnR
world (now I understand how people new to Java feel like...)

Thanks in advance,
Juan
Matthew Rudy J. (Guest)
on 2007-07-09 01:29
hi there Juan,

I can't quite see what you're trying to do.
The lovely module you're looking at is already included in Time.

So, if you want to do that,
just do Time.days_in_month(4, 2004)

gives you the number of days in april 2004

def days(cal)
    Time.days_in_month(cal.month, cal.year)
end

also,
maybe the original error was because your include should have been
inside the module.

Juan Medín wrote:
> Hi,
>
> I'm trying to use the module
> ActiveSupport::CoreExtensions::Time::Calculations in a helper module,
> but I get a nasty error. My module looks like this:

 days = self.class.days_in_month(month, year)
Stefan M. (Guest)
on 2007-07-10 01:14
(Received via mailing list)
Juan Medín wrote:
> Hi,
>
> I'm trying to use the module
> ActiveSupport::CoreExtensions::Time::Calculations in a helper module,
> but I get a nasty error. My module looks like this:
>
> require 'rubygems'
> gem 'activesupport'

This leaves activesupport unloaded.

The (now) prefered usage is:

require "rubygems"
require "activesupport" # picks up latest version of activesupport

or

require "rubgems"
gem "activesupport", someversion # defines which version to load
require "activesupport"

Granted, the message "require_gem is obsolete, use gem instead" is
misleading.

Just wanted to note this - I cannot help with your original message.

Stefan
This topic is locked and can not be replied to.