Having Fun w/ Ruby Gems
Tags: gems, firstyearincode, beginners, rubygems
Published: 2019-11-12 19:41:44

I recently began learning Ruby as my first programming language. Before I became interested in programming, I was an avid Wordpress user. When I first started using Wordpress, I had no idea what I was doing (similar to my experience with Ruby thus far). <br><br>![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/xdwkkvb3nw376181kxl5.png)<center><i>I think about this meme often</i></center>

What helped me become more comfortable with Wordpress was a thing called "plugins." A wordpress plugin is code that contains a group of functions that can help extend functionality on your site once they are installed. This is very similar to Ruby gems, which are packaged code that someone else has written to help save you time from coding the methods yourself.

<center><img src="https://cdn.wpbeginner.com/wp-content/uploads/2013/04/addplugins.png"></center>

The first time that a ruby gem caught my attention was when an instructor used the Faker Gem. The Faker gem helps generates random data, such as names and foods, to help populate the ruby program. Below is a quick example of Faker generating a random name:<center>![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/jghgnxbxw4m08k6z2mct.png)</center>
In order to output "Clemmie Nienow PhD" in my console, I simply followed five quick steps.

1) Create a <b>gemfile</b> file in my working directory, and add the following code to it: 
<b>source "https://rubygems.org"
gem 'faker'</b>

2) Next run <b>bundle</b> in the console in order to add to your project dependencies. Other people will now be able to clone your code and run <b>bundle</b> to include the project dependencies. If you don't have <b>bundler</b> installed on your system, follow these steps: https://help.dreamhost.com/hc/en-us/articles/115001070131-Using-Bundler-to-install-Ruby-gems

3) Add <b>require 'faker'</b> to the top of the file that you want (i.e. run.rb file).

4) Read the faker documentation to see how we can utilize Faker. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/53hi9lz3fbxy3qedt97o.png)


5) Run the ruby file (i.e. ruby bin/run.rb) and your done!


<img src="https://www.hcfama.org/sites/default/files/styles/content_col_top/public/homer-computer-woohoo.jpg?itok=mxv92uCf">


<h1>So what's next?</h1>
Now that we understand the basic idea of ruby gems, we can look through the gem libraries and install something new. First, we'll visit https://rubygems.org/. Rubygems.org is the main repository of ruby gems and includes over 10,000 gems.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0u49n5s75iumwqfdix6j.png)



Now let's search for a gem. Several of my friends are having birthdays coming up and I don't have any time to write them a card. Let's see if we can find a gem that will output a message for me. Let's search <i>Happy birthday</i>

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8y7f7bfxbvt4ufjcdsu0.png)This seems like a cool gem! We already know that we'll need to add it our <b>gemfile</b> and <b>'require'</b> it at the top of our code, but how do we utilize the gem? Let's check out the documentation link. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/q0a1bnsjju1p2pobjwok.png)

Let's create a new instance for bday and assign to the bday variable. Next line we'll just print the bday variable.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/5kk6hpeyghu93d6s2l2f.png)

<h1>Done!</h1>