# Including files from lib/

**URL:** <https://www.ruby-forum.com/t/including-files-from-lib/98816>\
**Category:** Rails\
**Created:** [July 18, 2006, 11:47am UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816 "2006-07-18T11:47:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 18, 2006, 11:47am UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/1 "2006-07-18T11:47:32Z")

</div>

I asked about this a few days ago and got no replies, so I’m asking  
again.

I have a file called string\_extensions.rb. In it, I extend the String  
class  
to include some extra functionality. I put this file into the lib/  
directory  
of my app.

But the changes made to String don’t take effect. The program acts as  
though  
the files aren’t being included.

I’ve stopped and restarted the server. No go. I tried including  
Reloadable.  
No go.

Is there something I’m missing? Seems like it should just work.

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 18, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/2 "2006-07-18T12:21:54Z")

</div>

“Rabbit” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
message  
news:removed\_email\_address@domain.invalid…

I asked about this a few days ago and got no replies, so I’m asking  
again.  
I have a file called string\_extensions.rb. In it, I extend the String  
class  
to include some extra functionality. I put this file into the lib/  
directory  
of my app.  
But the changes made to String don’t take effect. The program acts as  
though  
the files aren’t being included.  
I’ve stopped and restarted the server. No go. I tried including  
Reloadable.  
No go.  
Is there something I’m missing? Seems like it should just work.

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I had this problem too. My solution was to require the file in  
environment.rb  
also, my file was called string.rb, I don’t know if that will make a  
difference

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 18, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/3 "2006-07-18T12:46:20Z")

</div>

Rabbit wrote on 18.07.2006 11:46:

> I’ve stopped and restarted the server. No go. I tried including Reloadable.  
> No go.
> 
> Is there something I’m missing? Seems like it should just work.

Have you require the lib?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 21, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/4 "2006-07-21T20:28:59Z")

</div>

Hi Markus. Not sure what you mean by require the lib. I haven’t issued  
an  
explicit “require” statement, if that’s what you’re asking.

I’ll try what Alan suggested…

- Rabbit

* * *

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 21, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/5 "2006-07-21T20:35:41Z")

</div>

i just did this today actually. put this in your environment.rb:

require File.join(File.dirname( **FILE** ), ‘…/lib/string\_extensions’)

and it will be included. if you are still getting errors, post the  
String class you wrote.

ed

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 21, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/6 "2006-07-21T20:49:22Z")

</div>

Yah, Alan’s suggestion worked. I wonder why, if Rails automatically  
looks  
inside those directories I have to explicitly include them.

If anyone has any ideas or knowledge on the matter, please share. 🙂

- Rabbit

* * *

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 21, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/7 "2006-07-21T21:57:23Z")

</div>

On 7/21/06, Rabbit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Wow Ed. That’s a whole lot of work to include a file. Why not simply…

require “string\_extensions” is sufficient

> require “#{RAILS\_ROOT}/lib/string\_extensions”
> 
> What Alan suggested works fine, I just think it’s odd… why have those  
> directories… or maybe they’re NOT included by default, but simply provided  
> by default… as placeholders… Hmm…

lib is part of the load path, meaning that if you do require  
“file\_name” then it’ll search in the lib dir for the filename. It  
exists so that you can put stuff in there and just require it into  
your files without having to do any of that RAILS\_ROOT crap. Just  
like any Ruby app though, the file’s contents are loaded into memory  
until you require it.

Pat

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 21, 2006, 9:47pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/8 "2006-07-21T21:47:28Z")

</div>

Wow Ed. That’s a whole lot of work to include a file. Why not simply…

require “#{RAILS\_ROOT}/lib/string\_extensions”

What Alan suggested works fine, I just think it’s odd… why have those  
directories… or maybe they’re NOT included by default, but simply  
provided  
by default… as placeholders… Hmm…

Either way, thanks for the response Ed. By the way, my string extension  
is…

def each\_char  
each\_byte { |b| yield b.chr }  
end

- Rabbit

* * *

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [July 21, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/including-files-from-lib/98816/9 "2006-07-21T22:06:54Z")

</div>

AH! THANK YOU Pat! I see it now! And, after a bit of thought, it makes  
complete sense. 🙂

- Rabbit

* * *
