On Modules and require in general / on ActiveSupport in particular

N

nrolland

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"]
 
G

George Ogata

#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)

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top