Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The Gem Environment3:27 with Jason Seifer
In this video, we go deeper in to the gem environment and briefly explore how Ruby knows how to load a gem.
RubyGems does a few tricks when using a gem. 0:00 First off, gems are installed to a specific place on the file system. 0:04 This is usually your Ruby installation location, but it can also be changed. 0:09 Ruby then needs to modify the load path very slightly to take the gem location 0:14 into account. 0:19 Let's take a look at the gem environment now using workspaces. 0:21 If we take a look at our file system here, 0:26 we can see that we have money and money example.rb. 0:29 If we go up a level, we can see that there is a .local directory. 0:33 Now this is something that is work space specific. 0:40 So if I type Gem environment, we can see 0:43 that /home/treehouse/.local/gems is in our path. 0:48 This is where gems are installed to in a workspace. 0:54 We can see if we go into the gems directory. 0:59 We have our bin directory, build info, cache, extensions, doc, 1:03 gems and specifications. 1:07 This is where any gems that we install will go on the file system. 1:09 So here is the gems directory. 1:15 And we can see the different names of the gems that we have installed, 1:18 followed by their versions. 1:21 And you'll notice this corresponds to our gem list command. 1:24 And the gems that don't come with Ruby by default 1:29 are going to be installed right here. 1:32 So I'm gonna clear my screen here for a second and go into irb. 1:35 Now, when Ruby is loading a file it looks for 1:41 it in a special variable called the load path. 1:45 And we can actually look at the load path right now. 1:49 That's a constant that's going to be inside of every Ruby application. 1:53 And we can see the different places that Ruby searches for files to load. 1:58 Now, if we tried to use the Money gem right now, 2:03 You'll see we get uninitialized constant money. 2:10 But, when we require it. 2:13 This is all of a sudden, loaded and 2:19 we have access to the money class, because of the money gem. 2:22 So let's keep these load pads in line for a second right now, 2:28 and I'm going to clear my screen. 2:32 And print the load path to the screen again. 2:35 And you can see once we do that, Ruby has added to the front of this load path 2:38 array the money gem location and any of its dependencies. 2:45 Now as a file naming structre in a gem, most of the gem's library files 2:52 are in a lib directory below the main gem directory. 2:57 And that is what gets appended to the load path. 3:03 Since Ruby searches the load path for 3:07 different files to load, it now knows to look in these places first. 3:09 So, require will search your ruby gems location for 3:15 the name of the gem, and then take the most recent version and 3:19 append the lib directory if the gem exists. 3:23
You need to sign up for Treehouse in order to download course files.Sign up