Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

auto load

One thing I never looked too far into Rails was: what of my files in lib do I need to require in and what don’t I need to require in?

It turns out that if you follow some naming conventions you can avoid require‘ing files in, which can help keep your various environment files shorter.

If you have a class or module in lib who’s name follows the Ruby conventions of MixedCase and who’s file name is the lower case, words separated by underscores pattern, used by your Rails generated files, then it will be require‘d automatically.

For example, the following files will be require‘d automatically by Rails, there’s no need to require them in any environment file.

lib/query.rb
1
2
class Query
end
lib/string_extensions.rb
1
2
module StringExtensions
end

If you want to package your libraries in subdirectories in lib, those too can still be loaded automatically if you follow some conventions. Any directories must correspond to modules and you have to follow the same naming convention mentioned above.

For example, the following file will be require‘d automatically by Rails: lib/foo/bar.rb
1
2
3
4
5
6
module Foo

  class Bar
  end

end

About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.