# Requires

**URL:** <https://rubytalk.org/t/requires/73485>\
**Category:** ruby-talk\
**Created:** [24 February 2016 20:59 UTC](https://rubytalk.org/t/requires/73485 "2016-02-24T20:59:49Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bee.Lists](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bee.Lists](https://rubytalk.org/u/Bee.Lists)\
**Post date:** [24 February 2016 20:59 UTC](https://rubytalk.org/t/requires/73485/1 "2016-02-24T20:59:49Z")

</div>

Hi folks. Can someone provide some insight into how requires work with require\_relative?

HQ  
├── Gemfile  
├── Gemfile.lock  
├── app.rb  
├── [config.ru](http://config.ru)  
├── css  
│ ├── HQ.css  
│ └── reset.css  
├── img  
│ ├── traffic.jpg  
│ └── weather.gif  
├── alt  
│ ├── db.rb  
│ └── methods.rb  
├── blah.txt

In this model I would like app.rb to call methods.rb which calls db.rb when needed.

So in app.rb: require\_relative(‘/alt/methods.rb’)  
in methods.rb: require\_relative(‘db.rb’)

Does this work? I was having some issues before and I forgot the specifics of them, so I thought I would ask for a best practice on the list.

Any insight appreciated.

Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/rob_biedenharn/32/1859_2.png) [@Rob\_Biedenharn](https://rubytalk.org/u/Rob_Biedenharn)\
**Post date:** [24 February 2016 21:14 UTC](https://rubytalk.org/t/requires/73485/2 "2016-02-24T21:14:18Z")

</div>

Well, you want to use a relative path with require\_relative, so:

### app.rb  
require\_relative 'alt/methods'

### methods.rb  
require\_relative 'db'

Note that you don't typically see the .rb extension in require calls (Ruby can load .so files or the equivalent on some systems) and parentheses are rarely used for these either.

-Rob

> **···**
>
> > On 2016-Feb-24, at 15:59 , Bee.Lists \<bee.lists@gmail.com\> wrote:
> > 
> > Hi folks. Can someone provide some insight into how requires work with require\_relative?
> > 
> > HQ  
> > ├── Gemfile  
> > ├── Gemfile.lock  
> > ├── app.rb  
> > ├── config.ru  
> > ├── css  
> > │ ├── HQ.css  
> > │ └── reset.css  
> > ├── img  
> > │ ├── traffic.jpg  
> > │ └── weather.gif  
> > ├── alt  
> > │ ├── db.rb  
> > │ └── methods.rb  
> > ├── blah.txt
> > 
> > In this model I would like app.rb to call methods.rb which calls db.rb when needed.
> > 
> > So in app.rb: require\_relative(‘/alt/methods.rb’)  
> > in methods.rb: require\_relative(‘db.rb’)
> > 
> > Does this work? I was having some issues before and I forgot the specifics of them, so I thought I would ask for a best practice on the list.
> > 
> > Any insight appreciated.
> > 
> > Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [24 February 2016 21:51 UTC](https://rubytalk.org/t/requires/73485/3 "2016-02-24T21:51:53Z")

</div>

​There's always the documentation  
\<[Module: Kernel (Ruby 2.3.0)](http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-require_relative&gt;:)

> **···**
>
> On 25 February 2016 at 06:59, Bee.Lists \<bee.lists@gmail.com\> wrote:
> 
> > Hi folks. Can someone provide some insight into how requires work with  
> > require\_relative?
> 
> ---  
> &nbsp;&nbsp;&nbsp;Ruby tries to load the library named \*string\* \*relative to the requiring  
> file’s path\*. If the file’s path cannot be determined a LoadError is  
> raised. If a file is loaded true is returned and false otherwise.  
> ---  
> (my emphasis)
> 
> So you'd have this:
> 
> ~~~ app.rb  
> require\_relative 'alt/methods'  
> ~~~
> 
> ~~~ alt/methods.rb  
> require\_relative 'db'  
> ~~~
> 
> As Rob said in his reply, you don't need (and shouldn't include) the ".rb"  
> on the end of the required target. Think of it as \*requiring a library\*,  
> not \*loading a file\*. (Side note: if you do want to load a file, there's  
> Kernel#load \<[Module: Kernel (Ruby 2.3.0)](http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-load&gt;%5C))
> 
> Cheers  
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Bee.Lists](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bee.Lists](https://rubytalk.org/u/Bee.Lists)\
**Post date:** [24 February 2016 21:57 UTC](https://rubytalk.org/t/requires/73485/4 "2016-02-24T21:57:35Z")

</div>

Thanks for the replies, guys.

The issue with the documentation is that it doesn’t explain if a file that’s been called two files deep, will pertain outside of the first inclusion.

app.rb -\> inc/methods -\> db

Does db.rb persist in app.rb after that initial include?

> **···**
>
> > On Feb 24, 2016, at 4:51 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > ​There's always the documentation:
> > 
> > ---  
> > &nbsp;&nbsp;&nbsp;Ruby tries to load the library named string relative to the requiring file’s path. If the file’s path cannot be determined a LoadError is raised. If a file is loaded true is returned and false otherwise.
> 
> Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Quintus](https://avatars.discourse-cdn.com/v4/letter/q/49beb7/32.png) [@Quintus](https://rubytalk.org/u/Quintus)\
**Post date:** [24 February 2016 22:44 UTC](https://rubytalk.org/t/requires/73485/5 "2016-02-24T22:44:02Z")

</div>

"Bee.Lists" \<bee.lists@gmail.com\> writes:

> The issue with the documentation is that it doesn’t explain if a file  
> that’s been called two files deep, will pertain outside of the first  
> inclusion.

require\_relative treats its path always relative to the file it is  
written in, i.e. the currently active file, regardless of the  
depth level of requirements.

If /root/a.rb require\_relatives foo/b.rb, and that file require\_relatives  
bar/c.rb, then you end up with /root/foo/bar/c.rb being loaded. Just try  
it out.

Greetings  
Marvin

> **···**
>
> --  
> #!/sbin/quintus  
> Blog: [http://www.guelkerdev.de](http://www.guelkerdev.de)
> 
> GnuPG key: F1D8799FBCC8BC4F

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [25 February 2016 02:09 UTC](https://rubytalk.org/t/requires/73485/6 "2016-02-25T02:09:28Z")

</div>

​It says "  
relative to the requiring file’s path  
​", not "relative to the path of the first file that ever called  
require\_relative."

Think about it logically: why would I want to write a library/gem that uses  
'require\_relative' to load its parts, if all of that would break the first  
time someone loads my library/gem using require\_relative ?

> **···**
>
> On 25 February 2016 at 07:57, Bee.Lists \<bee.lists@gmail.com\> wrote:
> 
> > Thanks for the replies, guys.
> > 
> > The issue with the documentation is that it doesn’t explain if a file  
> > that’s been called two files deep, will pertain outside of the first  
> > inclusion.
> > 
> > app.rb -\> inc/methods -\> db
> > 
> > Does db.rb persist in app.rb after that initial include?
> > 
> > \> On Feb 24, 2016, at 4:51 PM, Matthew Kerwin \<matthew@kerwin.net.au\> \> wrote:  
> > \>  
> > \>  
> > \> ​There's always the documentation:  
> > \>  
> > \> ---  
> > \> Ruby tries to load the library named string relative to the requiring  
> > file’s path. If the file’s path cannot be determined a LoadError is raised.  
> > If a file is loaded true is returned and false otherwise.
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [25 February 2016 02:15 UTC](https://rubytalk.org/t/requires/73485/7 "2016-02-25T02:15:47Z")

</div>

Sorry, a second reply after a re-read of the question:

> app.rb -\> inc/methods -\> db
> 
> Does db.rb persist in app.rb after that initial include?

​I don't understand that question. The Ruby \*\*process\*\* is what "includes"  
all the code in those files. Once you require some code, that code is now  
part of the Ruby virtual machine state. The only way to get rid of it is  
for some other code to explicitly destroy (overwrite or remove all  
references to) it.

Note that Ruby doesn't have file scope either; global variables and  
constant are always global; class, instance, and local variables are bound  
to their class/instance/function.

Cheers

> **···**
>
> On 25 February 2016 at 07:57, Bee.Lists \<bee.lists@gmail.com\> wrote:  
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![thomas\_Perkins](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thomas\_Perkins](https://rubytalk.org/u/thomas_Perkins)\
**Post date:** [25 February 2016 02:20 UTC](https://rubytalk.org/t/requires/73485/8 "2016-02-25T02:20:33Z")

</div>

These are all great answers but everyone seems to be missing the point that you can give the full path to the file and it will load.

> **···**
>
> Sent from my iPhone
> 
> > On Feb 24, 2016, at 8:15 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > Sorry, a second reply after a re-read of the question:
> > 
> > > On 25 February 2016 at 07:57, Bee.Lists \<bee.lists@gmail.com\> wrote:
> > > 
> > > app.rb -\> inc/methods -\> db
> > > 
> > > Does db.rb persist in app.rb after that initial include?
> > 
> > ​I don't understand that question. The Ruby \*process\* is what "includes" all the code in those files. Once you require some code, that code is now part of the Ruby virtual machine state. The only way to get rid of it is for some other code to explicitly destroy (overwrite or remove all references to) it.
> > 
> > Note that Ruby doesn't have file scope either; global variables and constant are always global; class, instance, and local variables are bound to their class/instance/function.
> > 
> > Cheers  
> > --  
> > &nbsp;&nbsp;Matthew Kerwin  
> > &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Bee.Lists](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bee.Lists](https://rubytalk.org/u/Bee.Lists)\
**Post date:** [25 February 2016 13:12 UTC](https://rubytalk.org/t/requires/73485/9 "2016-02-25T13:12:54Z")

</div>

Think about what I said earlier. Outside of any logic, I was having issues with it earlier and wanted to know the scope of the code. If A calls B and B calls C (as in my case), I wanted to see how far C would remain active. There are languages where that scope is limited.

> **···**
>
> > On Feb 24, 2016, at 9:09 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > ​It says "relative to the requiring file’s path​", not "relative to the path of the first file that ever called require\_relative."
> > 
> > Think about it logically: why would I want to write a library/gem that uses 'require\_relative' to load its parts, if all of that would break the first time someone loads my library/gem using require\_relative ?
> 
> Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Bee.Lists](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bee.Lists](https://rubytalk.org/u/Bee.Lists)\
**Post date:** [25 February 2016 13:13 UTC](https://rubytalk.org/t/requires/73485/10 "2016-02-25T13:13:22Z")

</div>

OK, that’s what I was asking.

> **···**
>
> > On Feb 24, 2016, at 9:15 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > ​I don't understand that question. The Ruby \*process\* is what "includes" all the code in those files. Once you require some code, that code is now part of the Ruby virtual machine state. The only way to get rid of it is for some other code to explicitly destroy (overwrite or remove all references to) it.
> > 
> > Note that Ruby doesn't have file scope either; global variables and constant are always global; class, instance, and local variables are bound to their class/instance/function.
> 
> Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [25 February 2016 04:04 UTC](https://rubytalk.org/t/requires/73485/11 "2016-02-25T04:04:55Z")

</div>

​Indeed, as long as you're not writing a library that can be installed in  
different places​ on different users' machines, and will never be moved.

Hey, does anyone else remember the days before #require\_relative, when you  
had to use #require and either futz the load path, or do awesome things  
like:

&nbsp;&nbsp;&nbsp;&nbsp;require File.dirname(\_\_FILE\_\_).'/../lib/foo'

(or File.absolute\_path(...) rescue ... )

​Oh, another point everyone seems to be missing (?) is that #require and  
#require\_relative try to only load a file once, while #load reads, parses​,  
and executes it every time.

> **···**
>
> On 25 February 2016 at 12:20, thomas Perkins \<thomas.perkins23@icloud.com\> wrote:
> 
> > These are all great answers but everyone seems to be missing the point  
> > that you can give the full path to the file and it will load.
> > 
> > Sent from my iPhone
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [25 February 2016 04:06 UTC](https://rubytalk.org/t/requires/73485/12 "2016-02-25T04:06:42Z")

</div>

​This is a slight lie (sorry.) If you declare any local variables in the  
'global' scope of a file, those variables will not be available to the  
calling scope whether you use #load, #require, or #require\_relative, so  
there is a sort of file scope in that context.​

> **···**
>
> On 25 February 2016 at 12:15, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> 
> > Note that Ruby doesn't have file scope either; global variables and  
> > constant are always global; class, instance, and local variables are bound  
> > to their class/instance/function.
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Bee.Lists](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bee.Lists](https://rubytalk.org/u/Bee.Lists)\
**Post date:** [25 February 2016 13:15 UTC](https://rubytalk.org/t/requires/73485/13 "2016-02-25T13:15:42Z")

</div>

At no point was I ever asking about loading a file for any data reasons, etc. It was a question about the scope of inclusion of the functional code beyond a structure.

I also had the db.rb inclusion inside a method. Having executed that method once, I was interested to see if methods inside db.rb would still be accessible.

> **···**
>
> > On Feb 24, 2016, at 11:04 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > ​Oh, another point everyone seems to be missing (?) is that #require and #require\_relative try to only load a file once, while #load reads, parses​, and executes it every time.
> 
> Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [25 February 2016 21:02 UTC](https://rubytalk.org/t/requires/73485/14 "2016-02-25T21:02:34Z")

</div>

> At no point was I ever asking about loading a file for any data reasons,

etc. It was a question about the scope of inclusion of the functional code  
beyond a structure.

> 

That was a snark at Thomas Perkins, who said we were 'missing the point'  
about absolute paths.

> I also had the db.rb inclusion inside a method. Having executed that

method once, I was interested to see if methods inside db.rb would still be  
accessible.

> 

You probably should have said that up front, then people would know how to  
direct the answers.

For those sorts of uncommon edge cases, why not just construct a test  
script and see first hand how it works?

> \>  
> \> ​Oh, another point everyone seems to be missing (?) is that #require

and #require\_relative try to only load a file once, while #load reads,  
parses​, and executes it every time.

> **···**
>
> On 25/02/2016 11:16 PM, "Bee.Lists" \<bee.lists@gmail.com\> wrote:
> 
> > \> On Feb 24, 2016, at 11:04 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Bee.Lists](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bee.Lists](https://rubytalk.org/u/Bee.Lists)\
**Post date:** [25 February 2016 21:33 UTC](https://rubytalk.org/t/requires/73485/15 "2016-02-25T21:33:36Z")

</div>

What I did say was that I had forgotten the issues involved. I have done some messing around with some examples and then realized the require was within a method.

Essentially if a method is called specifically to load up db requirements, I was fuzzy if that connection was loaded for the rest of the script. I now know. I did construct something and it wasn’t playing out as expected. I might have been tired, like human beings get.

> **···**
>
> > On Feb 25, 2016, at 4:02 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> > 
> > You probably should have said that up front, then people would know how to direct the answers.
> > 
> > For those sorts of uncommon edge cases, why not just construct a test script and see first hand how it works?
> 
> Cheers, Bee

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [25 February 2016 22:37 UTC](https://rubytalk.org/t/requires/73485/16 "2016-02-25T22:37:32Z")

</div>

​Well, back to the original query (asking for best practices):

&nbsp;&nbsp;&nbsp;1. always use 'require'​ for system-wide libraries (gems, etc.)  
&nbsp;&nbsp;&nbsp;2. always use 'require\_relative' for local (sub-)libraries  
&nbsp;&nbsp;&nbsp;3. never use absolute paths  
&nbsp;&nbsp;&nbsp;4. don't include the ".rb" on the end of the filename unless you really  
&nbsp;&nbsp;&nbsp;mean it  
&nbsp;&nbsp;&nbsp;5. only use 'load' if you want all side-effects (definitions, etc.) to  
&nbsp;&nbsp;&nbsp;be (re)applied every time  
&nbsp;&nbsp;&nbsp;6. don't do any of the above from within a method unless you know what  
&nbsp;&nbsp;&nbsp;you're doing  
&nbsp;&nbsp;&nbsp;7. when in doubt, test it [1]

[1]: [ruby 'require' scope · GitHub](https://gist.github.com/phluid61/70c75302512630642d36)

> **···**
>
> On 26 February 2016 at 07:33, Bee.Lists \<bee.lists@gmail.com\> wrote:
> 
> > What I did say was that I had forgotten the issues involved. I have done  
> > some messing around with some examples and then realized the require was  
> > within a method.
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
