# Where to put require

**URL:** <https://www.ruby-forum.com/t/where-to-put-require/73009>\
**Category:** Ruby\
**Created:** [October 20, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/where-to-put-require/73009 "2006-10-20T16:26:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 20, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/where-to-put-require/73009/1 "2006-10-20T16:26:45Z")

</div>

If I have a rails app and I need to put in  
require ‘xxx’  
include yyy

in my class ApplicationController \< ActionController::Base

Where within this application.rb file do I put these lines? Thanks, Ike

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 20, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/where-to-put-require/73009/2 "2006-10-20T16:33:59Z")

</div>

On 10/20/06, Ike [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I have a rails app and I need to put in  
> require ‘xxx’  
> include yyy
> 
> in my class ApplicationController \< ActionController::Base
> 
> Where within this application.rb file do I put these lines? Thanks, Ike

It’s usually best to put it in the first lines of the file, before any  
variable declarations or class definitions. They’re a bit like C’s  
“” or Java’s “imports”.

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 20, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/where-to-put-require/73009/3 "2006-10-20T16:46:01Z")

</div>

On 10/20/06, Bira [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> variable declarations or class definitions. They’re a bit like C’s  
> “” or Java’s “imports”.

And be careful where you place include Yyy as the placement influences  
where the module will be mixed in.

It’s different when you do

include Forwardable  
class X  
…  
end

and

class X  
include Forwardable  
…  
end

Sometimes (=rarely), when you want to require later or not at all,  
it’s useful to put require inside a method.

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 20, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/where-to-put-require/73009/4 "2006-10-20T16:46:02Z")

</div>

Bira’s kind of right, kind of wrong.

Java imports are package based, which while there is the file system  
relation, is trying to keep developers from worrying about file paths.

Ruby requires work like C/C++ #includes. They are purely path related.  
However, Ruby does have a system variable available, $:, that is the  
PATH,  
so you can do something like this:

$: \<\< ‘…/…/lib/’  
require ‘some\_class\_in\_lib’

instead of  
require ‘…/…/lib/some\_class\_in\_lib’

You’ll see this type of syntax alot with libraries like Rails, who try  
to  
make everything available without any dealings with file _or_ package  
paths.

Jason

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 20, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/where-to-put-require/73009/5 "2006-10-20T20:41:11Z")

</div>

Jan S. wrote:

> Sometimes (=rarely), when you want to require later or not at all,  
> it’s useful to put require inside a method.

Just to make it a little clearer, by experimenting you can see:

* * *

def check  
a = [‘foo’, ‘bar’, ‘baz’] \* 2  
begin  
a.each\_slice(3) {|slice| p slice}  
rescue Exception  
puts “No such method\n”  
end  
require ‘enumerator’  
a.each\_slice(3){|slice| p slice}  
end

#Test starts here

begin  
x.each\_slice(3) {|slice| p slice}  
rescue Exception  
puts “No such method\n”  
end

puts $".include?(‘enumerator.so’)

check

puts $".include?(‘enumerator.so’)

## x = [‘foo’, ‘bar’, ‘baz’] \* 2 x.each\_slice(2) {|slice| p slice}

Produces -\>  
No such method  
false  
No such method  
[“foo”, “bar”, “baz”]  
[“foo”, “bar”, “baz”]  
true  
[“FOO”, “BAR”]  
[“BAZ”, “FOO”]  
[“BAR”, “BAZ”]

As you can see, ‘require’ is itself a method (btw, mixed with the  
require method of rubygems/custom\_require.rb if specified), that calls  
a library and adds it to $". Such library is called the first time is  
required. So, as Jan said, you may call it anywhere on your program  
depending on where do YOU require it.
