What is a good way to see output from my ruby gem?

98 Views Asked by At

I want to know a good way to run code in my ruby gem (and not just through tests).

That means I want to run ruby lib/{gemname}.rb on the terminal and be able to see some output

So I have this line in my base file:

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)

and then I load a file called debugger.rb by requiring it at the bottom of the base file. This file then gives me the output I need.

This works but this kind of clutters my code and I don't want to accidentally commit it and watch it break in production.

So what's a good way of doing this?

1

There are 1 best solutions below

0
On

My approach for developing and debugging gems has two parts:

1) Always use require_relative to include needed "internal" gem related files. This allows the gem to be loaded up normally when installed as a gem and also in my development environment (ignoring any versions of the gem that may already be installed.)

2) Then use the following snippet of code (usually) at the bottom of the main file, to activate debugging features when the base my_gem.rb file is run explicitly on the command line.

if __FILE__ == $0
  #debugging code goes here!
end

With this strategy there's no need to worry about yanking debug code before releasing the gem.