Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require_relative #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions project_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
<hr/>

[Go back to the
Expand Down