DEV Community

Jeremy
Jeremy

Posted on β€’ Originally published at blog.notgrm.dev

5

Use Bundler in a Ruby script

Sometimes when we write a Ruby script we need to use external gems to integrate with a third party API, or to facilitate the connection to a database.

For example, if you want to add coloring to produce messages

require 'rainbow'

puts Rainbow("My very important message in red").red
Enter fullscreen mode Exit fullscreen mode

But when deploying this script, how can we ensure that these dependencies will be present? And will they be installed in the expected version for which our script has been tested?

What to do then? Creating a gem seems too heavy for our script, and it's not advisable to expect users to do the installation by themselves (it's okay for a gem but for a script that would start having 4-5 of them it's a lot).

Fortunately, Bundler is there for us, in fact, the installer offers a module that we can include in our script and that offers all the features of a Gemfile but to define our gems inside the script.

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'

  gem 'rainbow', '~> 3.0.0'
end

puts Rainbow("My very important message in red").red
Enter fullscreen mode Exit fullscreen mode

Thus, when the script is launched, Bundler will check that the declared gems are installed, download them if necessary, and then load them automatically for use.

πŸ’‘ One last tip before you go

Tired of spending so much on your side projects? πŸ˜’

We have created a membership program that helps cap your costs so you can build and experiment for less. And we currently have early-bird pricing which makes it an even better value! πŸ₯

Check out DEV++

Top comments (4)

Collapse
 
fnordfish profile image
Robert Schulze β€’

I really like this approach and using it for years now. I find it especially useful with things like rubocop, that bring quite a dependencies list which might be conflicting with runtime gems.

Collapse
 
wilsonsilva profile image
Wilson Silva β€’

This is really cool. I wish that Ruby had a native way of handling dependencies like Deno has. We could reduce this code to:

require 'rubygems:rainbow@3.0.0'

puts Rainbow("My very important message in red").red
Enter fullscreen mode Exit fullscreen mode
Collapse
 
notgrm profile image
Jeremy β€’

Interesting idea, 🧐

Collapse
 
peterc profile image
Peter Cooper β€’

I use this in a variety of "script" type programs that I run only occasionally, such as to collect our data from Stripe and turn it into CSVs, or to process our PayPal logs, etc.

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay