Locked lesson.
About this lesson
How to install and use a third-party gem in your program.
Exercise files
Download this lesson’s exercise file.
Adding Third-party Functionality with Gems.docx60.4 KB
Quick reference
Adding Third-party Functionality with Gems
You can add third party functionality to your programs using gems.
When to use
Don't reinvent the wheel, whenever you can use code that's already been written - you should!
Instructions
Using a gem is a two-step process. Install the gem to your terminal, then reference it in your code.
To install a gem into your terminal:
gem install "gem_name"
To reference the gem in your program:
require "gem_name"
Hints & tips
- Install from the terminal: gem install "gem_name"
- Reference in your code: require "gem_name"
- 00:04 In this video I want to talk about adding third party functionality to your programs
- 00:08 with something called gems.
- 00:10 So one of the great things about Ruby is it has a huge community of programmers,
- 00:15 people that have been building little things that you can use for a long,
- 00:19 long time.
- 00:20 These things are called gems, and you can add them to your program.
- 00:22 It's sort of like class inheritance where you suck it in from somewhere else, and
- 00:26 you can use it, but a little bit different.
- 00:28 So they're called gems.
- 00:29 You know Ruby is a gem, so gem is like a little package that you can use.
- 00:33 It's a whole Ruby theme, I guess.
- 00:35 But to find these programs go to this website called RubyGems.org.
- 00:39 And you can see there's 14 billion downloads and counting, and
- 00:43 this is just a repository of gems that people have created that you can use
- 00:47 totally free in your program.
- 00:49 So I'm going to show you how to use these.
- 00:51 They're very easy to use, and we're going to search for
- 00:54 something called Faker, and Faker is a gem.
- 00:57 You can see it's been downloaded almost 15 million times.
- 01:00 Faker just generates random text, random stuff, fake data, right?
- 01:04 So we can play around with this.
- 01:06 But before we do, let me show you how to install these things.
- 01:09 It's a two step process.
- 01:10 You have to install the gem into your command line, and
- 01:13 then you have to pull the gem into your file.
- 01:15 So, first off, let's get rid of all this stuff.
- 01:18 And all you do is you come here to Ruby gems, and you look for the name,
- 01:22 and it's just right here.
- 01:23 This Faker, F-A-K-E-R.
- 01:25 So to install it just come to your command line, and type in gem install,
- 01:30 with spaces, and then inside the quotation marks just put the name of the gem.
- 01:35 So ours is faker, and if we hit enter you could see it's fetching the gem.
- 01:39 It's pulling it from rubygems.org, and you'll notice it says two gems installed.
- 01:44 A lot of times gems have dependencies.
- 01:46 They require other gems in order to use them, and they'll download and
- 01:49 install automatically, so it's no big deal.
- 01:51 So now this gem is installed in our system here, our C9 system, and
- 01:55 we can go ahead and use it by simply referencing it at the top of our program.
- 01:59 So I'm just going to go require, and then faker, and that's it.
- 02:04 Now we can use this gem.
- 02:05 So let's come over here and see what this gem is all about.
- 02:08 We can usually either look at the documentation or the homepage.
- 02:12 I'm going to go to the homepage, and
- 02:14 if we scroll down here this is all the stuff that Faker will do.
- 02:17 It will create fake data about Dragon Ball, food, friends, Game of Thrones,
- 02:21 Harry Potter, Hitchhiker's Guide to the Galaxy, all these different things,
- 02:25 Lord of the Rings.
- 02:26 And you can click through here and kind of look at this and read about this.
- 02:28 Some of this stuff is hilarious.
- 02:30 Let's look at this one.
- 02:31 Chuck Norris.
- 02:31 This will create a fake Chuck Norris fact.
- 02:35 So to use this all we have to do is grab this line, copy it, and
- 02:39 bring it back to our program, and let's say puts Faker: :ChuckNorris.fact.
- 02:45 Save this guy, run the program.
- 02:48 The only pattern Chuck Norris knows is God Object.
- 02:51 I have no idea what that means.
- 02:53 Let's run it again.
- 02:53 Chuck Norris' protocol design method has no status, requests or responses,
- 02:58 only commands.
- 02:58 I don't know what that means either.
- 03:00 So that's a Chuck Norris fact.
- 03:02 If we come back here, another one I kind of like Is, there we go,
- 03:06 Company, and then Company.bs.
- 03:09 I like this one, so let's try this one.
- 03:11 We just paste this in there, just save it, hit reload.
- 03:14 Revolutionize compelling solutions, seize enterprise technologies.
- 03:20 Weird business catchphrases, drive wireless portals.
- 03:24 Love it.
- 03:24 So it's just that easy to add functionality,
- 03:27 add these little programs to your program.
- 03:30 We could also say my variable equals, and
- 03:34 then puts out my VAR like that.
- 03:38 Once these things are in your program you can do anything you want with them, and
- 03:41 it's just that easy.
- 03:42 So come over here to rubygems, look around for different gem, I mean,
- 03:46 there's millions of gems that you can use.
- 03:48 They're all free.
- 03:49 You have to do a little searching to find,
- 03:51 there's some guides that can tell you more about this stuff.
- 03:54 Play around with this faker gem.
- 03:55 It's a lot of fun.
- 03:56 All kinds of weird stuff that you can do with it.
- 03:58 Just come through here, and just click on these things, and start reading.
- 04:01 How I Met Your Mother.
- 04:03 So just good stuff.
- 04:04 So that's all for this video.
- 04:06 In the next video we're going to start putting everything we've learned together,
- 04:09 and we're going to build a neat, little math flash card game.
- 04:12 A little app.
- 04:12 And so that'll be in the next video.
Lesson notes are only available for subscribers.