1 00:00:00,930 --> 00:00:03,220 Now that we know what gem we want to use and 2 00:00:03,220 --> 00:00:07,580 have it installed, we have to be able to use it in our program. 3 00:00:07,580 --> 00:00:10,090 Let's see how that works now using work spaces. 4 00:00:11,480 --> 00:00:14,220 So now that we have the money gem installed, let's go ahead and 5 00:00:14,220 --> 00:00:16,010 see how to use it in one of our programs. 6 00:00:17,020 --> 00:00:20,920 First, I'm actually going to scroll down the right side of the screen 7 00:00:20,920 --> 00:00:23,830 on the ruby gem documentation page, and 8 00:00:23,830 --> 00:00:29,410 I'm going to click the documentation link, and then we'll see what it gives us. 9 00:00:32,330 --> 00:00:37,040 Right now, I'm going to click on the API documentation, and 10 00:00:37,040 --> 00:00:40,580 then, let's see how to use it in one of our programs. 11 00:00:40,580 --> 00:00:46,070 Well, we've done the gem install, so let's go ahead and see how to use it. 12 00:00:46,070 --> 00:00:48,180 In this example, there's $10 US. 13 00:00:49,440 --> 00:00:51,812 Now first, we'll need to require the gem, 14 00:00:51,812 --> 00:00:56,630 s o let's go back to our work space and create a new file. 15 00:00:58,800 --> 00:01:01,069 We're going to call this money example.rb. 16 00:01:05,609 --> 00:01:10,760 Now once a gem is installed, the first thing that we need to do is require it. 17 00:01:10,760 --> 00:01:13,300 So, we’ll require money. 18 00:01:14,460 --> 00:01:17,590 If only it were that easy in life. 19 00:01:17,590 --> 00:01:20,960 Now that it’s required we can use it, so I’m going to go back and 20 00:01:20,960 --> 00:01:22,230 consult the documentation. 21 00:01:23,370 --> 00:01:27,640 It looks like we can initialize a new money instance by calling the class name 22 00:01:27,640 --> 00:01:32,650 money, and the word new, and then give it the number of cents in a currency. 23 00:01:33,900 --> 00:01:35,329 Let's go ahead and try that. 24 00:01:42,929 --> 00:01:46,490 And, for right now, let's just print that to the screen. 25 00:01:49,110 --> 00:01:51,650 Now, I'm gonna click down into the console, and run this program, 26 00:01:55,830 --> 00:01:58,290 and it gives us a money instance. 27 00:02:00,400 --> 00:02:04,690 So, at this point, we've installed a gem, and used it in a very simple program. 28 00:02:07,720 --> 00:02:11,498 Let's go ahead and create another one, more_money. 29 00:02:22,019 --> 00:02:23,740 And let's print this to the screen as well. 30 00:02:26,170 --> 00:02:29,680 Run this one more time, and 31 00:02:29,680 --> 00:02:33,730 we can see that we have successfully added two units of money together. 32 00:02:35,340 --> 00:02:40,430 And that's really all that's required to use a gem once we've got it installed. 33 00:02:40,430 --> 00:02:43,200 Just use the require statement. 34 00:02:43,200 --> 00:02:47,830 Ruby will then find the gem and import it for you in to your program. 35 00:02:49,070 --> 00:02:53,150 Generally, the require statement is going to be the same name as the gem. 36 00:02:54,180 --> 00:02:57,120 In this example it was called money. 37 00:02:57,120 --> 00:03:00,550 If we had installed another gem, such as multi jason, 38 00:03:00,550 --> 00:03:02,840 we would require multi jason instead.