For more information, please see full course syllabus of Introduction to Ruby
For more information, please see full course syllabus of Introduction to Ruby
Discussion
Study Guides
Download Lecture Slides
Table of Contents
Transcription
Related Services
Ruby Gems
- RubyGems is a package utility for Ruby
- Link: http://rubygems.org/
- Similar to Java's jar utility or Unix/Linux's tar utility
- RubyGems allows you to extend or modify functionality within Ruby applications
- RubyGems Manuals: http://docs.rubygems.org/
- Installing RubyGems: http://rubygems.org/pages/download
- Installing Rake: gem install rake
- Link: http://rubygems.org/gems/rake
- Creating Our First Gem
- Build it, install it, run it, and then publish it!
Ruby Gems
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.
- Intro 0:00
- RubyGems 0:08
- What are RubyGems?
- RubyGems.org 0:44
- How RubyGems are used
- Java's jar utility
- Unix/Linux's tar utility
- What is a Gem? 3:16
- Definition of Gem
- Version
- Date
- Author
- Description
- What Are the Uses? 4:18
- Uses for Gems
- Installation 5:06
- How to install RubyGems
- Updating to the Latest Ruby Gems 5:54
- Testing 6:22
- Example
- Installing Rake 7:24
- Example
- Verifying 9:22
- Example
- Structure 10:56
- gem.gemspec
- Specification 13:40
- What is in the gem?
- Who made it?
- Update gem version
- Example
- Create Our First Gem 17:20
- Steps involved
- RubyGems Guides
- Example
- Steps Review
- Create Our First Gem (Cont.) 23:08
- Building the gem
- Example
- Installing the gem
- Run it
- Publish it
- Get Some Gems! 25:06
- rake
- rails
- fastercsv
- koala
Computer Science: Introduction to Ruby
I. Introduction to Ruby | ||
---|---|---|
Setting Up Your Environment | 22:08 | |
Intro to Ruby | 22:20 | |
Basic Tools for Using Ruby | 27:44 | |
Ruby Specifics | 20:45 | |
Ruby Data Types (Part 1) | 29:37 | |
Ruby Gems | 25:50 | |
Ruby Data Types (Part 2) | 40:24 | |
Objects | 1:05:46 | |
Loops | 38:54 | |
Strings | 28:30 | |
Regular Expressions | 33:27 | |
Arrays | 14:35 | |
Hashes | 27:48 | |
Math Operations, Part 1 | 28:47 | |
Math Operations, Part 2 | 28:51 | |
Dates and Times | 26:01 | |
Methods: Part 1 | 31:24 | |
Methods: Part 2 | 20:11 | |
Classes: Part I | 26:51 | |
Classes: Part II | 26:42 | |
Classes: Part III | 53:36 | |
Modules | 24:19 |
Transcription: Ruby Gems
Welcome back to educator.com.0000
Today's lesson will be on Ruby Gems.0002
What are Ruby Gems? It's a package utility for Ruby--think of it as--you have different packages, like tar, zip, compressed files--well, the package utility for Ruby...that is what Ruby Gems does.0005
It has the exact same feature for that.0024
Let's go ahead and look at RubyGems.org, so we can see more of what they are, and get a good feel of how it is being used.0026
This is the RubyGems.org site.0039
There are tons of downloads here--865 million.0044
There are a lot of different Gems.0050
You can use these Gems in your projects; you can put them into your project and use them for your own codes, so you're not reinventing the wheel.0051
You're using this code for your own benefit, and you can concentrate on the stuff that matters.0063
There are some links here: there's one that says "Learn"; it will teach you about Ruby Gems by clicking that.0069
There is a share button, and there is "Install RubyGems 1.8.2.4."0078
You can browse the guides--it's explanations, tutorials, references...and Gem specifications.0083
We are going to go over all this stuff; we want to make sure you get used to these RubyGems.0090
This is a very popular thing in Ruby.0095
Here is...to update your Gems...gem update --system...you can build your Gem using gemspec...and you can push your Gem to this website.0098
It does require you signing up for an account, but then you can create your own Gem, and you push it, and it will be out there for everyone to use it.0109
Notice, they also give you a list of what the most-downloaded Gems today were, what was just updated, and some of the newer Gems out there.0116
Before we go any further, make sure, at the top right, you do sign up.0131
Make sure you sign up for the site; then you can keep track of the Gems you've downloaded and the ones you are pushing--uploading.0135
Notice, here are all the "Learn" buttons...we will go through them later, but let's go back to our slide now.0144
Ruby Gems has their own packages for different languages.0161
I put here Java JAR--that is Java's version of Ruby Gems--the JAR function.0168
And UNIX and Linux have the tar utility.0178
These are just a couple of them; the other languages do have their own, also.0182
These are more of the common ones I've seen.0191
So, what is a Gem? Like I said, it's a package--we've already gone over that--and it contains all the necessary files and information to run on this system.0195
In these Gems themselves, they will have version information.0206
It will also have the date it was created.0219
Author is quite common; the description of what the Gem is; and there are more pieces of information, too.0227
What is the use of these Gems?0255
Extend or modify functionality within your Ruby application.0258
Avoid duplication: you don't want to be creating code that someone else has already created; why don't you go with a Ruby Gem...find that piece of code, see if someone has already developed it and just pull that in.0267
You can make their Gem better--be a contributor and help out.0280
Again, stop reinventing the wheel.0284
Fourth, follow Ruby's open source foundation--strong open source foundation--help contribute.0288
Installation: we are going to go over how to actually install Ruby Gems.0303
If you have already looked at our installation lesson, and you have RVM set up, it's already set up; so you can actually skip this and go on to the next lesson.0310
But this is still good information, so if you want, you can look at this, too.0322
Otherwise, you go back to that site that we showed you, download the source--it's going to come as tar/gzip file--uncompress that, go into the directory, and just do ruby setup.rb.0325
That will get you set up with RubyGems.0344
Now, how do you know if you have it installed?0348
First, to update to the latest RubyGems, you just run gem update --system.0352
This gem command will actually be existing on your system after it's installed.0361
So, you could do gem --version, and it will show you the version of it.0366
Here we go: we can run through some of the commands now, too.0371
So, if I do gem -v, it gives me the version, and when I do gem --version, it also comes up; when I do gem in this, I get a lot of usage information about it, too.0395
And this is the RubyGem sophisticated package manager.0410
I see this with gem--and gem help will actually show you the exact same information.0423
And gem --version is just to verify that you installed their correct one.0437
Now, we can have an example of setting up a Gem.0443
One that you should already have it, if you don't already, is the Rake Gem.0448
That's gem install rake.0452
We can go to the site and take a look at that.0457
Right here, in this search box, I'm going to put "rake."0466
It gives me a lot of Gems that have used Rake for their dependencies, and they add features to it, but we just want the main one.0473
I'm going to get the Rake one--Rake 0.9.22.0484
It has the download link, documentations and stats...you can subscribe to follow the Gem and track what progress is being made...0491
It says that the total number of downloads was quite a lot, in the number for this version.0501
You can actually see the Gem following the version.0508
It doesn't really show you anything about how to install it, but it's so easy--it's just gem install rake; it's right there.0515
It's already installed, but we can actually go ahead and try it now; let's see what information it gives us.0526
It should say it has already been set up, but...0532
It's a very simple command--gem install rake--and it will show some information there.0535
While that's running, let's keep going, though.0549
So, verifying the Gems--you can see what Gems are installed with Gem List.0564
We should actually do that; let's take a look at that and make sure it's set up.0572
For this task, look for the Rake and the version number--there's a rake command where we can verify that.0577
Then, just run rake --version.0587
I'm going to quit out of this; it's going kind of slow, but let's check.0595
And there you go--it has already been installed: Rake 0.9.22.0602
Rake --version...there it is...it's already set up; I'll just do rake and see if it gives me some usage information.0607
There you go--if I have my rakefile on here, I can go ahead and run it.0615
That's good...let's see if there's a rake help here...there is a rake help file, too, to show you how it's used.0620
rake...and included the specified, explicit filename with -f.0635
The options are below.0640
The next part is: we want to go over the structure to create your Ruby Gem.0650
It's a very common structure that is used, and it's very standard, so please follow it.0658
If you do add extra common things to it, those will just be additions; so this structure should be followed.0667
First, you also have...whatever Gem you have--the directory that holds everything.0674
It doesn't matter what name you give it.0684
What is really important is this gem.gemspec file, because that is going to be the specifications that everything is set in.0687
First, what happens is all your code is going to go to this Lib directory, which is a subdirectory to Gem.0696
You don't have to specify your file as gem.code; it could be whatever name you give it.0705
As long as your specifications say that file is included, it will include that file.0713
And we are going to go over the specs, so you can take a look at them.0720
Test is for unit testing; it's recommended to use it as you're doing your first one.0724
You're probably still learning it, so...0732
For our uses, we're not going to do any unit tests right now.0739
Readme--very self-explanatory: if I get your Gem, I look at the sourcecode, I want to look at your readme to see how I set it up, a description, installation...any tips or common things I should know.0747
Your rakefile--if there are any tasks that are common that you created to run your Gem--anything I need to load--you can specify in your rakefile.0764
This is also optional.0776
So, this test and this rakefile, and even this readme, these things are all optional, but recommended.0778
And the last one is the gem.gemspec file.0796
Depending on the name you give your Gem, this will change, but whatever name you give it, that should be the name that is placed here and even in your code.0799
OK, to the specifications: What is in the Gem?0819
You can include the name of it, a description of what it is, the version, who made it--include your author information...the developer...0824
And, as you continue to build the specification, you want to continue to update it and update the version for it.0839
Let's go ahead and do that now--let's create our first Gem!--how about that?0848
Let's start with the specifications.0853
I actually want to...get another code base...OK, so we have our structure in a lib directory.0862
The first thing we want to do is create the spec file.0873
gemspec...so the first thing is you want to create your Gem specifications.0878
The first thing I'm going to put is the actual name of this Gem.0897
I'm just going to call it Educator.0900
And then, I'm going to specify the version; for this version, I'm going to start very, very low.0905
Here is the date we are using.0919
Let's just make this like a hello world app--how about that.0937
Authors--for authors you can specify multiple--I'm just going to specify myself, but you can continue to add more to the list.0943
Now, for s.files, this is very important that you include all of your files to this directory.0964
So this one--we're only going to use one file in this.0974
Too big...and the homepage...they will actually give you a place to load your Gems; in this case, it's called Educator.0978
I'm going to show you the path it is being uploaded to, so you can see--you could even download it yourself.0989
OK--we have our Gem specification done.0997
We have more than enough information for it.1000
Really, you need to require the version, author, and the name, because those are needed for the Gem to be generated out.1007
OK, we've saved that; if you look in there, we have our educator.gemspec.1021
Let's go back to the slide now.1029
We have our specification done; next, let's look at the guide to creating our first Gem.1034
Guides.rubygems.org...1044
OK, I have it here.1050
Well, we already know what a Gem is; we want to make our own now.1054
Introduction...our first gem...required files...executables...tests...documenting your code...1063
Even here, you will notice they are fairly simple.1074
There is structure; they have a .gemspec file; the code is in the lib directory.1079
It even says, reading through this file, "Package is placed within the lib directory"; once your Gem is loaded, you just do require, and it will run it.1087
They have their specifications here.1100
When you have created a gemspec, you can build your Gem and install it.1104
Let's go ahead and try it out.1110
I want to actually go in the directory to create it.1121
Let's go back to the slide first...so we know all the tasks for it--that is our next thing; we need to build it.1130
After we implement all the code--this includes developing the code for it--then we can install it, run it, and publish it.1142
Our main thing will be this build step, where we have to create the code.1159
After that, all these are things that are more simple to do.1167
Let's go to work on our first step to build this Gem.1174
Since we are calling it Educator, it's going to actually be called educator.gemspec.1179
Let's get some code for that now.1196
Make sure we're in the right directory...now let's touch educator.rb, class Educator, and we'll have just a very simple hello world.1200
OK, excellent: so we have our code file; we have our .gemspec.1232
Now, let's try to build it.1238
It says no description was specified; but it still built the Gem.1245
Warning...and we didn't include a description, so let's do that.1253
Let's try it again now...excellent: we don't get the warning.1271
Now, we go to the file itself: Educator-0.0.1.gem.1275
The next thing is, we want to actually install this gem now that we've done the build step.1286
So, what this command does: gem install educator-0.0.1; it's going to install this Gem into my RubyGems.1295
I can actually use this Gem in my code.1305
There you go: it's installed--I have my Gem installed, I have some documentation--I didn't actually set up any, but we've gone through the RDoc lesson, so I'll let you guys do that.1310
So, now I can just go to require 'educator'--it says "true"--then I specify the actual code, and there you go: it prints "hello world."1322
We've built it, installed it, and now we can publish it to the world if we wanted to.1332
We went through this step; gem install rubygem-0.0.1--this version will change, depending on what you put in the .gemspec, and this will also change.1352
Again, to run it, since it's already in your Gem directory, you can require it--as simple as that: just putting the name of it in.1371
The next step would be, "Hey, I have this great code piece; I want to publish it to the world! I'm going to help other people use it; I already developed it--why don't I help others and maybe, if I have some feature I would like to have with it, but I don't have time, maybe someone else could work on it, too!"1385
This step is to publish it.1403
To do this, you would sign up on RubyGems.org, and then they have a command to help you publish it.1407
It's back on their site...it's exactly like the slide--you run this curl command, it goes to this URL--you will specify a username when you sign up for your account, and now it's going to ask you for that password that you created with the user.1417
Once you do that, you just run gem push, and it will actually push it to the website, and your Gem will be up.1448
Notice here, you are doing this curl command; change this to your name; and then this URL will stay the same, and you just put in this and the curl with the Gem; you put in your credentials, and you put your password here.1456
Then, all you have to do is just run this gem push, and it will push the Gem to RubyGems.org, and you will see it by the message: it will say, "Successfully registered"--your Ruby Gem is online now.1481
Other than that, I would say just start looking at those Gems and downloading some.1505
We've looked at Rake; another popular one is the Rails Gem; FasterCSV is a Gem you can use to parse CSV files, and it's quite popular, and it does the job really well.1512
Then this one called Koala will allow you to interact with the Facebook API and start using Open Graph.1531
So, that is the lesson today at Educator.com.1542
You just learned RubyGems.1545
Join us again for the next lesson!1548
Start Learning Now
Our free lessons will get you started (Adobe Flash® required).
Sign up for Educator.comGet immediate access to our entire library.
Membership Overview