Forum: Ruby load a path (not a file)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Elias O. (Guest)
on 2009-02-20 09:18
Hello guys,

I need to load a path not a file. I am trying this but it is not working

load RAILS_ROOT+"/public/users/#{self.login}/lib/"

Can you guys help me,

Thanks
Stefano C. (Guest)
on 2009-02-20 09:28
(Received via mailing list)
Alle Friday 20 February 2009, Elias O. ha scritto:
> Hello guys,
>
> I need to load a path not a file. I am trying this but it is not working
>
> load RAILS_ROOT+"/public/users/#{self.login}/lib/"
>
> Can you guys help me,
>
> Thanks

What do you mean by "load a path"? A path isn't a ruby file, so you
can't load
it. If you mean "load all ruby files contained in a directory", you can
do
something like this:

dir = RAILS_ROOT+"/public/users/#{self.login}/lib/"
Dir.entries(d).each  do |f|
  load File.join(d, f) if file.match(/\.rb$/)
end

Stefano
Tietew (Guest)
on 2009-02-20 10:06
(Received via mailing list)
On Fri, 20 Feb 2009 16:26:33 +0900
In article <removed_email_address@domain.invalid>
[Re: load a path (not a file)]
Stefano C. <removed_email_address@domain.invalid> wrote:

> > I need to load a path not a file. I am trying this but it is not working
> >
> > load RAILS_ROOT+"/public/users/#{self.login}/lib/"

It is NOT recommended to put ruby files under Rails "public" directory
because their files can be shown by any web browsers in the world.

> dir = RAILS_ROOT+"/public/users/#{self.login}/lib/"
> Dir.entries(d).each  do |f|
>   load File.join(d, f) if file.match(/\.rb$/)
> end

Dir.[] or Dir.glob is easier.

Dir.glob("#{RAILS_ROOT}/public/users/#{self.login}/lib/*.rb").each { |f|
load(f) }
Elias O. (Guest)
on 2009-02-20 10:14
Thanks guys,

That was very helpful and just what I wanted.

I will try to move the files to another folder for security then.

Elías
This topic is locked and can not be replied to.