Using a module within another module


#1

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:inremove_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:inremove_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:inreset_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:indispatch’”,
“/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:inservice’”, “/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in
service'", "/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’”,
“/usr/local/lib/ruby/1.8/webrick/server.rb:173:in start_thread'", "/usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’”,
“/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start'", "/usr/local/lib/ruby/1.8/webrick/server.rb:92:instart’”,
“/usr/local/lib/ruby/1.8/webrick/server.rb:23:in start'", "/usr/local/lib/ruby/1.8/webrick/server.rb:82:instart’”,
“/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:inrequire’”,
“/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:innew_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:inrequire’”, “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


#2

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)


#3

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