0

Requires single file is working fine:

require Rails.root.join('lib/railsadmin_custom_functions', 'drive_clear.rb')

--

Now I'm trying to automate and require all files on the same folder:

Dir[Rails.root.join('lib/railsadmin_custom_functions')].each {|file| require file }

So I'm getting this error:

dependencies.rb:229:in `require': cannot load such file -- /Users/me/projects/myproject/lib/railsadmin_custom_functions (LoadError)

Am I doing something wrong?

2 Answers 2

2

Your code for load railsadmin_custom_functions as a file.

'lib/railsadmin_custom_functions'

change to

'lib/railsadmin_custom_fuctions/*.extention_of_files'

Try this

Dir["#{Rails.root}/lib/railsadmin_custom_functions/*.rb"].each {|file| require file }

or

Dir.glob("#{Rails.root}/lib/railsadmin_custom_functions/*.rb")

output

=> ["/Users/me/projects/myproject/lib/railsadmin_custom_functions/file1.rb", "/Users/me/projects/myproject/lib/railsadmin_custom_functions/file2.rb"]

Or try this, if you want to require files in all folders and files in subfolders

Dir.glob("#{Rails.root}/lib/railsadmin_custom_functions/**/*.rb")
0
0

You have to skip . and .., which are entries of a directory listing. You might also want tn check if the entry is a directory and skip them as well:

Dir[Rails.root.join('lib/railsadmin_custom_functions')].each do |file|
  next if File.directory?(file)
  require file
end
1
  • don't understand. can you give some example? Sep 12, 2014 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.