Ruby Gem Basics → As a New Developer

Ruby Gems can be an exciting and at times confusing higher level of abstraction for up and coming developers. These powerful packages of code allow for the quick implementation new features to our programs, while also granting the opportunity to learn pivotal new concepts from a community of fellow programmers.

RubyGems vs Ruby Gems?

What are they?

Are reusable chunks of code that can be added into a program/project that often serve a specific purpose. These groups of code are a standard concept amongst most other languages and also quite often referred to as ‘libraries’.

The DRY principle:

DRY stands for: Don’t Repeat Yourself, and it is a fundamental that every developer lives by. A task that is repeated several times within the same method, class, directory, program and so on.. can and should be condensed into a broad definition of code that can be used at every similar instance. Doing so saves time, avoid redundancy and provides a form of linear functionality across multiple use cases.

Gems at their core do this, by sparing the programmer from writing out entire sets of code to add a specific feature to their program and instead import it. Once a Gem is installed, we now have all those pre-written methods at our fingertips to work with.

Why use gems?

Pros:

  • Increased abstraction
  • Increased consistency
  • Automation of reusable functionality across several programs / projects
  • Decreased time spent hard coding features and/or debugging

Cons:

  • Using too many gems can cause conflict gems, not all gems were made to work together
  • Too many gems and not enough of your own code can cause you to lose understanding of your project and its functionality
  • Too much time spent looking for the right gem

In the end, it’s quite generally best to use a healthy mix between your own code and several other gems.

How to Gem?

  • How to implement Gems

To learn how to use a gem, we first have to dive into what a gem is made of. All gem are made of this standard organization:

Lets break this down:

  • The lib directory has all the code for the gem
  • The test (sometimes are called spec) directory holds the tests
  • The bin directory holds an executable file that’s loaded into the user’s PATH when the gem is installed
  • The Rakefile is used by the rake program to write some extra code, perform separate tasks and automate tests
  • The README contains documentation by the creator about the gem
  • The gemspec contains more detailed information on the gem such as: the name, version number, platform, author(s) information, etc.

Now that we understand all the pieces of a ruby gem we can install them. While working within our application, the install command will download and install the gem (as well as any additional necessary dependencies).

Every application has a $LOAD_PATH, which controls how our Ruby code if found via the require statement(s). When requiring a gem, we are placing the gem’s lib directory into the load path. Now, when running our Ruby program, it looks for the required keywords, identifies the specified gems, and runs them when running our program. Therefore, having that code (and it’s methods) available for use with the program.

That’s it! We have successfully installed and required our gems and we are ready to make use of them as our application requires.

What Now?

One of the greatest benefits of using gems is the ability to be a part of a community of developers constantly adding adding and improving ruby code to be shared all over the world.

Key Resource: https://rubygems.org/

RubyGems is Ruby’s primary gem hosting service. Here you can:

  • Become a contributor and publish your own built gems
  • Find new gems & install them

I also encourage you to dive deeper into rails & bundler. These additions onto our vanilla ruby environment make it more easier install, keep record, and work with out ruby gems moving forward.

Any Questions?

CREDITS

https://medium.com/@charlotte.pearce1984/blogging-2db9749cef7c

image: https://quietresonance.bandcamp.com/track/interweb

image: https://portswigger.net/daily-swig/popular-ruby-gem-strong-password-backdoored

image: https://www.shareicon.net/