Coincidental to John Vincent’s SysAdvent entry, Always Be Hacking, I just completed my first Sinatra web app. It wasn’t a big app (36 lines, plus a tiny bit of HTML), nor a particularly important or impressive one (it searches a file for matching strings). But it turned out to be a useful exercise.
Generally, I treat all programming tasks as exercises to be left to … well, someone more conscientious than me. Read the text, perused the sample code, got what I need thanks. I like programming, at least at a scripting level, but it’s an activity that demands Flow, whereas system administration is frequently an activity that demands your Flow be wrecked. It’s an unequal battle, and I opted some years ago to surrender and go without the Flow - or, as far as possible, the programming.
But I had some time on my hands and was feeling a bit unchallenged, and I’d been reading up on Sinatra in a kind of idly curious, “how do these newfangled web apps work?” way when I was hit with a (rare) good idea for a demo app that would also scratch an itch. So, for once, I bothered to code it; took about a day, looks the dog’s and, rather than leaving all my knowledge as untainted, impractical theory, actually taught me a few useful things:
- How to create a modern CSS layout as simply as possible (using Twitter’s Bootstrap toolkit).
- How to grep files and interpolate variables within regular expressions in Ruby. (Clue: ignore all examples of reading the file to check every line, and just use the File.grep method. With Regexp.quote.)
- How to obtain file modification time in Ruby, and how to use Time objects.
- What layouts and ‘before’ filters do in Sinatra, and how to implement partials in templates with a helper.
- How to swallow newlines in ERB templates using trim.
- Where to obtain mod_passenger for RHEL and how to deploy Sinatra apps under Apache, including the use of a config.ru file.
- How incredibly easy it is to accomplish trivial tasks by simply cutting and pasting code snippets from Google.
Next time (?), I reckon I could make the whole thing even quicker by perhaps learning some Ruby too.