Gems
Gems are ruby libraries that can be easily reused across projects. A large number of gems exist to help developers leverage the code written by other developers. Gems are designed to be easily portable across projects.
The frameworks Ruby on Rails and Sinatra are just gems themselves.
How to use gems
There exist gems for pretty much anything that you may need in your Ruby code: authentication, games development, security, communications, etc.
Let's take a look at a particular example of using a certain gem. For example, let's pretend we want to send a text message from our Ruby program. Text messages can be sent by Twilio, a communications provider that developer its own gem called twilio-ruby for us to use.
First, we'll need to install it. In your command line type
$ gem install twilio-ruby
This will download it from the internet (from RubyGems.org, to be exact) and install into your system. Gem installations are specific to your ruby version, so if you install a gem forruby-2.0.0
and then switch toruby-1.9.3
, you'll need to install it for that version as well.
#There is video instructions from twilio which you can follow
#The app can be run by typing in 'ruby first-sms-app.rb' or
#'bundler exec ruby first-sms-app.rb'
#vide link: https://www.twilio.com/docs/quickstart/ruby/sms#faq
require 'bundler'
Bundler.require()
#put your credentials here, get them at twilio.com
account_sid = "ACfghdhj67628237827hj2h9h98d338"
auth_token = "ee782781u3hjk3ku83hd8839d89h9d8"
#set up a client to talk to the twilio REST API
@client = Twilio::REST::Client.new(account_sid, auth_token)
#send an sms
@client.messages.create(
from: "+44twilio_number",
to: "+44verified_number",
body: "I\'ve learnt about a new technology this morning"
)
Provided that you have the credentials (account ID and authentication token) from Twilio, this code is everything you need to send a text message from your Ruby code.
The first line is necessary to load the gem into memory. This makes the classes defined inside the gem available to our program. In particular, after the gem has loaded, we get access toTwilio::REST::Client
class that we instantiate on line 8 and use on line 11.
How to install gems
There are two ways of installing gems. First, you can install them manually:
$ gem install name_of_the_gem
This works but the problem with this approach is that the information about the gems you're using isn't available to other developers.
For example, let's say you wrote an awesome program that sends text messages and you put it on github for others to use. However, there's no easy way for other developers to know what gems you used in your code. If we're talking about a short script such as the one above, the answer may be obvious but real projects usually use dozens of various gems, so installing all of them manually isn't an option.
Another problem with the manual approach is that when you deploy your application, that is, copy it to a server for everyone to use, you may not be able (or want to) install the gems manually.
A better way of installing and managing gems is to use bundler. Bundler is a program (and a ruby gem as well) that is used to manage other gems. You install it manually (gem install bundler
) but you use it to install and manage all other gems.
To use bundler, you need to tell it what gems you're using in your project. To do this, create a file calledGemfile
(with capital G) in the root directory of your project. A simpleGemfile
may look like this:
source
'https://rubygems.org'
gem
'twilio-ruby'
The first line tells bundler to download the gems from RubyGems.org (the source of the gems in the Ruby world). The second line tells bundler that you want to use thetwilio-ruby
gem in your project. After you create the file, execute the following command
$ bundle install
This will download and install the latest version of the gem from RubyGems.org, as well as other gems that twilio-ruby may need. Then, in your application you'll need to set up bundler before requiring any gems:
require
'bundler/setup'
After this call, you'll be able to require the gems as we've done in the script above.
Where to find gems to use
A great place to start is Ruby Toolbox, a directory of ruby libraries and frameworks, ranked by popularity. Otherwise, a google search often gives good results. Try searching for "ruby send text gem" andtwilio-ruby
will probably be one of the top results.
What a gem looks like inside
A gem is simply a set of Ruby classes that you require in your code just like any other Ruby code. You may take a look at twilio-ruby source code on github.
When you do
require
'twilio-ruby'
you are actually requiring thelib/twilio-ruby.rb
file that, in turn, loads all other files. To see what a gem looks like, clone this repository and take a look in the lib folder.