Hi Guys, I would like to require a set of *.rb files into my test. But the problem is they sit in sub-directories. So i end up with a whole list of require statements. If i am doing something like this: Dir["lib/config/*.rb"].each {|file| require file } Dir["lib/common/*.rb"].each {|file| require file } Dir["lib/page/*.rb"].each {|file| require file } lib is common so is there a way I can turn those three statements into just one line of code? So that it looks like something like this: Dir["lib/*/*.rb"].each {|file| require file } any help would be much appreciated... Kind regards, Usman Hussain -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 25, 8:54 am, Usman Hussain <usmanhhuss...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Guys, > I would like to require a set of *.rb files into my test. > But the problem is they sit in sub-directories. So i end up with a > whole list of require statements. > > If i am doing something like this: > Dir["lib/config/*.rb"].each {|file| require file } > Dir["lib/common/*.rb"].each {|file| require file } > Dir["lib/page/*.rb"].each {|file| require file } > > lib is common so is there a way I can turn those three statements into > just one line of code? > > So that it looks like something like this: > Dir["lib/*/*.rb"].each {|file| require file }This will require every file in lib and its subdirectories: files = File.join(File.dirname(__FILE__),''..'',''lib'',''**'',''*.rb'') Dir.glob(files).each do |file| require file end If you must have it as a one liner (a little harder to read imho): Dir.glob(File.join(File.dirname(__FILE__),''..'',''lib'',''**'',''*.rb'')).each{| file| require file} This assumes you''re keeping your directory structure organized something like: |~project_root/ | |~lib/ | | |+config/ | | |+common/ | | |+page/ | |~test/ | | |-test.rb -T -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Usman Hussain
2011-Feb-28 09:17 UTC
Re: How to require all directories and sub-directories
ahhh brilliant. Thanks for that. I did something a bit similar but had all the folder names in an arry getting passed in. But that was a bit messy looking. So your solution works a treat. Thanks for your help. Kind regards, Usman Hussain On Feb 26, 5:07 am, "t.pickett66" <t.picket...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 25, 8:54 am, Usman Hussain <usmanhhuss...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi Guys, > > I would like to require a set of *.rb files into my test. > > But the problem is they sit in sub-directories. So i end up with a > > whole list of require statements. > > > If i am doing something like this: > > Dir["lib/config/*.rb"].each {|file| require file } > > Dir["lib/common/*.rb"].each {|file| require file } > > Dir["lib/page/*.rb"].each {|file| require file } > > > lib is common so is there a way I can turn those three statements into > > just one line of code? > > > So that it looks like something like this: > > Dir["lib/*/*.rb"].each {|file| require file } > > This will require every file in lib and its subdirectories: > files = File.join(File.dirname(__FILE__),''..'',''lib'',''**'',''*.rb'') > Dir.glob(files).each do |file| > require file > end > > If you must have it as a one liner (a little harder to read imho): > Dir.glob(File.join(File.dirname(__FILE__),''..'',''lib'',''**'',''*.rb'')).each{| > file| require file} > > This assumes you''re keeping your directory structure organized > something like: > |~project_root/ > | |~lib/ > | | |+config/ > | | |+common/ > | | |+page/ > | |~test/ > | | |-test.rb > > -T-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.