DEV Community

CalvinJimenez
CalvinJimenez

Posted on

Gems in Ruby

Image descriptionGems in Ruby are, in my opinion at least, one of the coolest features about Ruby. Ruby gems generally have a specific purpose such as faking data or something more general like testing/debugging your code. To start you'd have to install bundler by doing:

install bundler
Enter fullscreen mode Exit fullscreen mode

Then the way you'd import a gem is by simply running:

gem install (your ruby gem)
Enter fullscreen mode Exit fullscreen mode

The final step is to install the gem by running:

bundle install
Enter fullscreen mode Exit fullscreen mode

A way to find gems is by looking through the official website: https://rubygems.org/. The coolest part about gems has to be the fact that if you're thinking of doing something tedious or complex to some degree then someone has probably already made it into a gem. A personal favorite gem of mine has to be the faker gem. I alluded to it earlier, it's a gem that fakes data. It can give you different things ranging from fake characters from lord of the rings to fake street names and addresses. The syntax for this gem is:

Faker::Name.name

Faker::Address.street_address

Faker::Movies::LordOfTheRings.character
Enter fullscreen mode Exit fullscreen mode

The text after the colon specifies what kind of information you want and then the dot notation says what you want from that broad range of information. It's pretty expansive and can be used to mainly fake data for a personal project.

Another super popular gem is the Pry gem. This gem can be used in tandem with another gem known as the rake gem. Simply put, both gems are used for testing out your code and managing it easier. You enter the rake console by running:

rake console
Enter fullscreen mode Exit fullscreen mode

This puts you in a pry session, which is used to run code in specific blocks. There are of course rules you have to follow, but once you get the hang of them it becomes a very powerful tool when you're testing your code. Ruby gems are my favorite aspect of ruby for this exact reason, there's just an endless amount of possibilities whether it be something wacky or something invaluable.

Top comments (0)