# "require" behaves differently in Rails vs vanilla Ruby?

**URL:** <https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313>\
**Category:** Rails\
**Created:** [March 18, 2010, 12:26am UTC](https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313 "2010-03-18T00:26:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poco\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@Poco\_R](https://www.ruby-forum.com/u/Poco_R)\
**Post date:** [March 18, 2010, 12:26am UTC](https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313/1 "2010-03-18T00:26:43Z")

</div>

I can “require” a file from the console but not from irb or a simple  
ruby script. It’s not a path issue, the same file is found in both  
cases. So why is the module namespace an unrecognized constant  
sometimes and a valid module name others?

Same .rb file both times. This one works:

[chrisa@ibs-chrisa-ux1 lims\_m6.6]$ script/console  
Loading development environment (Rails 2.3.5)

> > require ‘aibs/hotfix/logger’  
> > [2010-03-17 13:50:02] INFO AIBS::Hotfix::Logger initialized at  
> > module load time  
> > =\> []
> 
> > exit

… but this one bombs with a NameError.

[chrisa@ibs-chrisa-ux1 lims\_m6.6]$ irb  
irb(main):001:0\> require ‘aibs/hotfix/logger’  
NameError: uninitialized constant AIBS  
from /home/chrisa/depot/tech/lims\_m6.6/lib/aibs/hotfix/  
logger.rb:1  
from /local1/home/chrisa/rs\_base\_dev/ruby/lib/ruby/site\_ruby/  
1.8/rubygems/custom\_require.rb:31:in `gem_original_require' from /local1/home/chrisa/rs_base_dev/ruby/lib/ruby/site_ruby/ 1.8/rubygems/custom_require.rb:31:in`require’  
from (irb):1  
irb(main):002:0\> exit

---

<div class="post-metadata">

**Author:** ![Poco\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@Poco\_R](https://www.ruby-forum.com/u/Poco_R)\
**Post date:** [March 18, 2010, 12:29am UTC](https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313/2 "2010-03-18T00:29:04Z")

</div>

On Mar 17, 8:56 pm, Poco R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can “require” a file from the console but not from irb or a simple  
> ruby script. It’s not a path issue, the same file is found in both  
> cases. So why is the module namespace an unrecognized constant  
> sometimes and a valid module name others?

When you run script/console a lot more is loaded that if you just run  
irb (eg plugins are loaded, rails is loaded, application initializers  
are loaded). In addition const\_missing is hooked which will  
automatically require stuff when a missing constant is found. Either  
of these could explain the difference between the two cases

Fred

---

<div class="post-metadata">

**Author:** ![Poco\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@Poco\_R](https://www.ruby-forum.com/u/Poco_R)\
**Post date:** [March 18, 2010, 12:53am UTC](https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313/3 "2010-03-18T00:53:39Z")

</div>

Thanks for helping me understand this. I get that lots of stuff gets  
loaded via the console, it certainly takes longer 😉

Here’s what’s really puzzling. It seems like the syntax is changing.  
Could this be an effect of the const\_missing hook? For example, I  
don’t even have to load a file. The console can handle the scoping on  
one line:

[chrisa@ibs-chrisa-ux1 lims\_m6.6]$ script/console  
Loading development environment (Rails 2.3.5)

> > module AIBS::Hotfix::Logger  
> > end  
> > =\> nil

but irb can’t:

[chrisa@ibs-chrisa-ux1 lims\_m6.6]$ irb  
irb(main):001:0\> module AIBS::Hotfix::Logger  
irb(main):002:1\> end  
NameError: uninitialized constant AIBS  
from (irb):1  
irb(main):003:0\> [chrisa@ibs-chrisa-ux1 lims\_m6.6]$

what’s even weirder, instead of the ::**:: form, I nest them on  
different lines and voila:**

 

[chrisa@ibs-chrisa-ux1 lims\_m6.6]$ irb  
irb(main):001:0\> module AIBS  
irb(main):002:1\> module Hotfix  
irb(main):003:2\> module Logger  
irb(main):004:3\> end  
irb(main):005:2\> end  
irb(main):006:1\> end  
=\> nil

 

Yikes!

 

This is all driven by a desire to use a bunch of (our) library code  
outside of the Rails framework i.e. for backend processing…

 

Any pointers you could suggest that would help me get a grip on what’s  
going on would be greatly appreciated.

 

On Mar 17, 4:27 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Poco\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@Poco\_R](https://www.ruby-forum.com/u/Poco_R)\
**Post date:** [March 18, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313/4 "2010-03-18T22:29:44Z")

</div>

Ah. Thanks, this makes (more) sense now.

On Mar 18, 2:05 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Poco\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@Poco\_R](https://www.ruby-forum.com/u/Poco_R)\
**Post date:** [March 18, 2010, 10:06am UTC](https://www.ruby-forum.com/t/require-behaves-differently-in-rails-vs-vanilla-ruby/185313/5 "2010-03-18T10:06:09Z")

</div>

On Mar 17, 11:52 pm, Poco R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> different lines and voila:

That’s not unexpected - the second form reopens (or creates) the AIBS  
module, whereas writing AIBS::Hotfix::Logger requires the AIBS and  
AIBS::Hotfix already exist. If they don’t exist then const\_missing is  
hit. Rails hooks this to try and load AIBS from somewhere (as long as  
there is a file named aibs.rb in one of the search paths this should  
work), apparently succeeding, whereas the default implementation just  
throws NameError

> This is all driven by a desire to use a bunch of (our) library code  
> outside of the Rails framework i.e. for backend processing…

The easy way here is to require config/environment which loads up  
rails for you. If you just want the const autoloading you can set that  
up to but you’ll have to delve into the Rails startup stuff to see how  
it initializes stuff.

Fred
