Including files from lib/

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.

“Rabbit” [email protected] wrote in
message
news:[email protected]

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
[email protected]
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

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?

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

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

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. :slight_smile:

  • Rabbit

On 7/21/06, Rabbit [email protected] 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

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

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

  • Rabbit