How to add interactive console to your Ruby app?
I’ve been writing some Ruby app lately (the kind that runs in the terminal and doesn’t include
Webrick as http server) and I found myself missing some of the tooling that is available with Rails - especially the command line.
The ability to just type
rails c and play around with your code was one of the framework killer-features
back in the days:
Here’s how to do it in Ruby console application :)
Our app will contain only three files
We will follow Rails conventions and put our executables in the
And that’s it - we can now type
bin/console and our
Cat class is available!
It wouldn’t feel like a real console without being able to call
reload! so let’s try to add it!
First we need to change
application.rb because the former will not load
our source file again if it has been already loaded.
And then we will add
reload! function to our script: