[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

On Modules and require in general / on ActiveSupport in particular

nrolland

12/2/2006 4:29:00 PM

I dont get how :

-> I require_gem ActiveSupport and it's not loading any ActiveSupport
module
-> I require_gem Rails and it's loading
ActiveSupport module.....

Any clue appreciated.


Here is a transcript of my irb session
#initial state
require 'rubygems'
=> true
a = Module.constant

# I load a gem, the Modules gets loaded
require_gem 'needle'
=> true
b = Module.constant
b - a
=> ["Queue", "Needle", "Mutex", "ScanError", "Logger", "StringScanner",
"SizedQueue", "Monitor", "MonitorMixin", "ConditionVariable"]

# I load ActiveSupport, nothing gets loaded !!!!!
require_gem 'activesupport'
=> true
c = Module.constant
c -b
=> []


# I load Rails, ActiveSupport is loaded. where is th magic going on ?
require_gem 'rails'
=>true
d = Module.constant
(d - c).find_all { |t| t.downcase.include?("active")}
=> ["ActiveSupport", "ActiveRecord"]

1 Answer

George

12/4/2006 2:25:00 AM

0

On 12/3/06, nrolland <nicolas.rolland@gmail.com> wrote:
> #initial state
> require 'rubygems'
> => true
> a = Module.constant

Hmmm, I'm not sure where you got #constant from... it's usually #constants .

> # I load ActiveSupport, nothing gets loaded !!!!!
> require_gem 'activesupport'
> => true
> c = Module.constant
> c -b
> => []

AFAIK, #require_gem isn't normally used anymore. What it does is add
the gem directory to the load path, and then require all its
"autorequires". Problem is, activesupport doesn't specify any
autorequires, so it doesn't do anything.

I believe usually, you just "require 'rubygems'", and then require
files in any gems as you need them. In activesupport's case, you want
the file named 'active_support.rb'. So this should do the trick:

require 'rubygems'
require 'active_support'

Even more commonly, requiring rubygems is done through the RUBYOPT
environment variable (set it to 'rubygems'), and not in the code
itself. This way, you don't rely on the library being provided
through a gem.

> (d - c).find_all { |t| t.downcase.include?("active")}
> => ["ActiveSupport", "ActiveRecord"]

Shorter:

(d - c).grep(/active/i)

:-)