1 00:00:00,930 --> 00:00:03,650 RubyGems does a few tricks when using a gem. 2 00:00:04,750 --> 00:00:09,200 First off, gems are installed to a specific place on the file system. 3 00:00:09,200 --> 00:00:13,800 This is usually your Ruby installation location, but it can also be changed. 4 00:00:14,820 --> 00:00:19,350 Ruby then needs to modify the load path very slightly to take the gem location 5 00:00:19,350 --> 00:00:19,900 into account. 6 00:00:21,010 --> 00:00:24,150 Let's take a look at the gem environment now using workspaces. 7 00:00:26,540 --> 00:00:28,340 If we take a look at our file system here, 8 00:00:29,750 --> 00:00:33,490 we can see that we have money and money example.rb. 9 00:00:33,490 --> 00:00:40,270 If we go up a level, we can see that there is a .local directory. 10 00:00:40,270 --> 00:00:43,280 Now this is something that is work space specific. 11 00:00:43,280 --> 00:00:46,770 So if I type Gem environment, we can see 12 00:00:48,090 --> 00:00:53,310 that /home/treehouse/.local/gems is in our path. 13 00:00:54,460 --> 00:00:57,380 This is where gems are installed to in a workspace. 14 00:00:59,290 --> 00:01:01,550 We can see if we go into the gems directory. 15 00:01:03,130 --> 00:01:07,910 We have our bin directory, build info, cache, extensions, doc, 16 00:01:07,910 --> 00:01:09,920 gems and specifications. 17 00:01:09,920 --> 00:01:14,020 This is where any gems that we install will go on the file system. 18 00:01:15,570 --> 00:01:17,330 So here is the gems directory. 19 00:01:18,610 --> 00:01:21,650 And we can see the different names of the gems that we have installed, 20 00:01:21,650 --> 00:01:22,940 followed by their versions. 21 00:01:24,110 --> 00:01:28,060 And you'll notice this corresponds to our gem list command. 22 00:01:29,420 --> 00:01:32,850 And the gems that don't come with Ruby by default 23 00:01:32,850 --> 00:01:34,380 are going to be installed right here. 24 00:01:35,770 --> 00:01:39,680 So I'm gonna clear my screen here for a second and go into irb. 25 00:01:41,770 --> 00:01:45,440 Now, when Ruby is loading a file it looks for 26 00:01:45,440 --> 00:01:49,600 it in a special variable called the load path. 27 00:01:49,600 --> 00:01:53,000 And we can actually look at the load path right now. 28 00:01:53,000 --> 00:01:56,970 That's a constant that's going to be inside of every Ruby application. 29 00:01:58,310 --> 00:02:02,620 And we can see the different places that Ruby searches for files to load. 30 00:02:03,870 --> 00:02:06,613 Now, if we tried to use the Money gem right now, 31 00:02:10,292 --> 00:02:13,820 You'll see we get uninitialized constant money. 32 00:02:13,820 --> 00:02:15,770 But, when we require it. 33 00:02:19,720 --> 00:02:22,800 This is all of a sudden, loaded and 34 00:02:22,800 --> 00:02:27,000 we have access to the money class, because of the money gem. 35 00:02:28,800 --> 00:02:32,060 So let's keep these load paths in mind for a second right now, 36 00:02:32,060 --> 00:02:34,430 and I'm going to clear my screen. 37 00:02:35,810 --> 00:02:38,330 And print the load path to the screen again. 38 00:02:38,330 --> 00:02:45,550 And you can see once we do that, Ruby has added to the front of this load path 39 00:02:45,550 --> 00:02:50,830 array the money gem location and any of its dependencies. 40 00:02:52,560 --> 00:02:57,500 Now as a file naming structure in a gem, most of the gem's library files 41 00:02:57,500 --> 00:03:01,970 are in a lib directory below the main gem directory. 42 00:03:03,040 --> 00:03:06,140 And that is what gets appended to the load path. 43 00:03:07,320 --> 00:03:09,730 Since Ruby searches the load path for 44 00:03:09,730 --> 00:03:13,590 different files to load, it now knows to look in these places first. 45 00:03:15,010 --> 00:03:19,500 So, require will search your ruby gems location for 46 00:03:19,500 --> 00:03:23,860 the name of the gem, and then take the most recent version and 47 00:03:23,860 --> 00:03:26,470 append the lib directory if the gem exists.