From 449e7101d3b07e5157f683ee32de298fbc59b831 Mon Sep 17 00:00:00 2001 From: madebydna Date: Sun, 22 Jan 2012 22:01:01 -0500 Subject: [PATCH 1/2] info on require vs. require_relative --- README.md | 1 + project_layout.md | 7 +++++++ 2 files changed, 8 insertions(+) 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..7aee4af 100644 --- a/project_layout.md +++ b/project_layout.md @@ -25,6 +25,13 @@ 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 determines the proper relative paths to the referenced files based on the current working directory. This lies in contrast to the `require` method which references files relative to the directory the program is executed from. + +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. Nonetheless, as a 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 within a source tree without polluting 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).
[Go back to the From 9d8fcfb423ff7e2a537b91f4b85b1435d9b62008 Mon Sep 17 00:00:00 2001 From: madebydna Date: Mon, 23 Jan 2012 08:40:56 -0500 Subject: [PATCH 2/2] correction of require vs. require_relative --- project_layout.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/project_layout.md b/project_layout.md index 7aee4af..04ea489 100644 --- a/project_layout.md +++ b/project_layout.md @@ -27,9 +27,11 @@ Tools like RVM and RBENV make life a whole lot easier when it comes to managing ## Use `require_relative` whenever possible -`require_relative` dynamically determines the proper relative paths to the referenced files based on the current working directory. This lies in contrast to the `require` method which references files relative to the directory the program is executed from. +`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. Nonetheless, as a 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 within a source tree without polluting the load path. +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).