Console Your Gems: Add REPL to Them
When you are working on a Ruby gem or a Ruby library ( why you would have a Ruby library that isn’t a gem is beyond me ), it is
always often desirable to have a Pry session loaded with the gem your working on. In fact, I’ll go out on a limb and say that REPL driven development is a a must-do when writing libraries. REPLs are like ice added to your beer when it isn’t cold anymore, except this ice is made from the same beer.
A lot of gems ship with this functionality, but if not, it is extremely easy to add one. To avoid polluting every gem with our code, we can utilize the concept of Rake’s global tasks. Rake’s default source for looking at tasks or rules is the Rakefile and all files that are declared as source files in the Rakefile; typically
*.rake files in your tasks folder, but this can vary depending on your project. However, rake also looks at
~/.rake/*.rake if we ask it to. So let’s create a file called
~/.rake/console.rake and add the following task to it:
1 2 3 4 5 6 7 8 9 10
And run our shiny new rake task:
1 2 3 4 5
Bummer! Let’s double check:
Nothing! What gives? Not a cause of worry, because this is the expected behaviour, and we have a failing test. Rake takes global pollution quite seriously, and does not load the global tasks unless asked to. So let’s ask rake to do so, by adding a
-g flag. Because, you know, g for global:
1 2 3 4 5 6 7 8
With this, I bow out and promise to come back with more palatable content later.