diff --git a/README.md b/README.md index f8fb6d6..793a712 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ We've compiled a list of common quality issues encountered during the session an - Using Bundler - Organizing your files - Managing your environment + - Use require\_relative whenever possible ## [Code Structure Notes](https://github.com/mendicant-university/s10-notes/blob/master/code_structure.md) diff --git a/project_layout.md b/project_layout.md index 4d23d57..04ea489 100644 --- a/project_layout.md +++ b/project_layout.md @@ -25,6 +25,15 @@ The general directory layout of Ruby libraries and applications has been fairly Tools like RVM and RBENV make life a whole lot easier when it comes to managing your Ruby environment. However, if you check in your `.rvmrc` file or `.rbenv-local` files, you may end up forcing a contributor/reviewer to either use your preferred way of configuring things or jump through hoops to override your configuration file in their own environment. An alternative approach is to treat these files the same you would any other configuration file: add them to your `.gitignore` and provide an example configuration file. It makes sense to get in the habit of doing this for anything that might affect the user's environment, including things like your `Guardfile` if you're using [guard](https://github.com/guard/guard). +## Use `require_relative` whenever possible + +`require_relative` dynamically generates the correct path to a referenced file relative to the working directory. This allows you to simply reference other files relative to the location of the file in which the `require_relative` statement is mentioned. The `require` method works altogether differently in that it searches through the directories listed in the `$LOAD_PATH` array. + +Before `require_relative` existed, most people either had to resort to ugly `File.join` hacks, or manipulate the load path in some way to get `require` to work as you would intuitively expect. As a result, many tools including Rake, Rubygems, Rspec, etc. added `lib/` to the load path by default so that these workarounds would not be necessary. Even in these special cases where `require` would work just as well, it's still better to use `require_relative`. + +As rule of thumb, when faced with two options that both accomplish the same thing, you should chose the one that has less potentially harmful side effects. In this case, `require_relative` is the better choice because it provides a clean syntax to do requires that are decoupled from the load path. + +For more in-depth information on the differences between `require` and `require_relative` and other ways to load code, you should read the Practicing Ruby article [Ways to load code](http://practicingruby.com/articles/shared/tmxmprhfrpwq).