# Require in 1.9.2 and 1.9.1

**URL:** <https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506>\
**Category:** Ruby\
**Created:** [May 27, 2011, 4:22am UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506 "2011-05-27T04:22:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 27, 2011, 4:22am UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506/1 "2011-05-27T04:22:06Z")

</div>

#i have a file ‘user.rb’ in the current directory. if i …

require “user”

#i get this messege (1.9.2 in xp and in windows 7)

##internal:lib/rubygems/custom\_require:29:in `require’: no such file  
to  
##load – user\_input (LoadError)

## from internal:lib/rubygems/custom\_require:29:in `require’

##this worked fine in 1.9.1 in xp. and when i

puts $:

#in 1.9.1 the last load path is ‘.’, but ‘.’ is not in load path for  
#1.9.2  
#works fine if i

require\_relative ‘user’

#or if i

load ‘user.rb’

#a bug or a reason for this behavior? i used the installer. on xp and  
on #windows 7

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [May 27, 2011, 4:55am UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506/2 "2011-05-27T04:55:27Z")

</div>

On Thu, May 26, 2011 at 9:22 PM, Ill E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #or if i
> 
> load ‘user.rb’
> 
> #a bug or a reason for this behavior? i used the installer. on xp and  
> on #windows 7
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

It’s intentional, because “.” is relative. So your script would work if  
you  
invoke it from the same dir on 1.9.1, but would break if you invoke it  
from  
the parent dir, because now it is looking in the parent dir for “user”  
rather than the dir of the file. So most code that does require  
statements  
like that has a bug there.

IDK what everyone else does, but for me, if its a bigger project, I add  
the  
project root dir to the load path so that require(“user”) would then  
work if  
user.rb is in the root dir of the project.

For smaller projects, like 1 or 2 files, I just do  
require(“#{File.dirname  
**FILE** }/user”).

In library code (ie a gem you are writing), I don’t think you are  
supposed  
to alter the load path as part of the lib at all, rather do that in your  
test code or in your binary, or just rely on rubygems to do it for you.  
Then  
within the library, you can require things relative to the lib directory  
of  
your project, even though the lib itself doesn’t alter the path.

I’d be interested to know whether everyone else does it like this also,  
or  
if you guys have discovered some useful pattern I don’t know about.

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [May 27, 2011, 5:26am UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506/3 "2011-05-27T05:26:32Z")

</div>

one method to require them all:  
require\_relative

ok, maybe not ‘all’ but it will solve your problem, as long as you’re  
using  
1.9. and if you want to be _sure_ it will work you can do something  
like:

if RUBY\_VERSION \< ‘1.9’  
Kernel.class\_eval do  
def require\_relative str  
require ‘./’ + str  
end  
end  
end

in your top level lib that gets called first & then you can have all the  
require\_relative’s you want 😃

require\_relative ‘foo’  
require\_relative ‘bar/bak’  
require\_relative ‘animals/magical/fire\_breathing\_llamas’

# and so on…

have fun!!  
hex

p.s. the method i made above is probably seriously cheating & may have  
some  
flaws that one more knowledgeable than i could point out, but ATM it  
should  
work well enough.

On Thu, May 26, 2011 at 10:55 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ##load – user\_input (LoadError)  
> > require\_relative ‘user’
> 
> project root dir to the load path so that require(“user”) would then work  
> within the library, you can require things relative to the lib directory of  
> your project, even though the lib itself doesn’t alter the path.
> 
> I’d be interested to know whether everyone else does it like this also, or  
> if you guys have discovered some useful pattern I don’t know about.

–

> > Other than the fact Linux has a cool name, could someone explain why I  
> > should use Linux over BSD?
> 
> No. That’s it. The cool name, that is. We worked very hard on  
> creating a name that would appeal to the majority of people, and it  
> certainly paid off: thousands of people are using linux just to be able  
> to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the  
> mistake of putting a lot of numbers and weird abbreviations into the  
> name, and is scaring away a lot of people just because it sounds too  
> technical.  
> – Linus Torvalds’ follow-up to a question about Linux

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [May 27, 2011, 6:05am UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506/4 "2011-05-27T06:05:44Z")

</div>

On Thu, May 26, 2011 at 10:26 PM, serialhex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end  
> end

According to the spec (  
[https://github.com/rubyspec/rubyspec/blob/master/core/kernel/require\_relative\_spec.rb#L20](https://github.com/rubyspec/rubyspec/blob/master/core/kernel/require_relative_spec.rb#L20)),  
require\_relative  
requires relative to the file rather than current dir. I think this  
means it  
would have to be handled by the interpreter because I don’t think there  
is a  
way to get the file of the caller.

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [May 28, 2011, 1:06am UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506/5 "2011-05-28T01:06:40Z")

</div>

On Fri, May 27, 2011 at 9:01 AM, Steve K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> I wrote a gem that backports require\_relative (which is the correct way to  
> do it!) to Ruby 1.8: [GitHub - steveklabnik/require\_relative: This backports require\_relative to Ruby 1.8. · GitHub](https://github.com/steveklabnik/require_relative)
> 
> Having “.” in the $: is a security concern. And modifying $: is bad  
> practice, in general. That’s Rubygems’ job.

spiffy!!! thanks steve!  
and yeah, don’t use my metaprogramming cheat, steve’s is MUCH better!  
(and  
much less copy-paste-error prone 😃 )  
hex

–

> > Other than the fact Linux has a cool name, could someone explain why I  
> > should use Linux over BSD?
> 
> No. That’s it. The cool name, that is. We worked very hard on  
> creating a name that would appeal to the majority of people, and it  
> certainly paid off: thousands of people are using linux just to be able  
> to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the  
> mistake of putting a lot of numbers and weird abbreviations into the  
> name, and is scaring away a lot of people just because it sounds too  
> technical.  
> – Linus Torvalds’ follow-up to a question about Linux

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [May 27, 2011, 3:02pm UTC](https://www.ruby-forum.com/t/require-in-1-9-2-and-1-9-1/207506/6 "2011-05-27T15:02:18Z")

</div>

I wrote a gem that backports require\_relative (which is the correct way  
to  
do it!) to Ruby 1.8: [GitHub - steveklabnik/require\_relative: This backports require\_relative to Ruby 1.8.](https://github.com/steveklabnik/require_relative)

Having “.” in the $: is a security concern. And modifying $: is bad  
practice, in general. That’s Rubygems’ job.
