Ruby Prototyping

Prototyping in Ruby

Once in a while I decide to work on a Ruby idea that is intended to be small. Not every Ruby project has to be Ruby on Rails. Maybe it's a local script to do something useful. Maybe it's something destined to be a gem. Maybe I have no idea what it's going to be, but I want to experiment.

After getting used to test triven development (TDD), it's hard to start working on something without a test file. What's next is the decision whether to lay out a proper project, start a gem, etc... Instead of going down this road, I like to just set up a single ruby file that includes everything. If this experiment turns out to be something I want to keep, it's easy enough to refactor it later. Executing this script/test combo should run the tests and usually, but not always, do nothing else.

Add minitest-spec to the Ruby source file

 1 module Something
 2   ...
 3 end
 4 
 5 if __FILE__ == $0
 6 
 7   require 'minitest/spec'
 8   require 'minitest/autorun'
 9   require 'mocha/setup'
10 
11   describe Something do
12     it 'should have tests'
13   end
14 
15 end

Run the test straight from Vim

I want to be able to run this from Vim, see the results, and return to Vim like nothing happened. First, mark the script as executable:

chmod +x [name of script]

Then make sure you have this command in your ~/.vimrc

map <leader>, :!ruby %<CR>

Then from Vim with the relevant script in the current buffer, execute the script and the test. Since my map leader is comma, the command above can be run with "comma comma".

,,
comments powered by Disqus