I recently updated my vim script for running rspec specs from vim.
Roll Your Own Remember Me
I’ve been setting up Fluid Applications for a lot of the web applications I use frequently. This lets you run a web application in a lightweight browser, like it’s a native OS X application. Instead of having Gmail, Google Reader, Pivotal Tracker, and New Relic running in tabs in a browser I have them running as separate applications that I can switch between using the normal Cmd-Tab.
This has been working really well for me, until I hit one annoyance with New Relic. This web app doesn’t have a remember me feature. Its login cookies expire every time you close your browser session (as is the default in ruby on rails). This meant that every time I would pop it open I would have to re-enter my login credentials. Turns out I like to quit and reopen this application a lot, and was starting to get really irritated by having to retype my email and password to do a quick check of the production servers.
ack: the fast code search tool
One of the handiest tools in my programming toolkit is ack, a Perl script that is great for searching through code (or really any text files). I use it a lot while refactoring, for example when I want to rename a method every time it’s called in a project.
ack with this command (from http://betterthangrep.com/):
$curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 ~/bin/ack
Assuming that ~/bin/ack is in your path searching code is as easy as this:
$ ack my_poorly_named_method
How To Run Rspec Specs From Vim
Test Driven Development is all about having a quick feedback loop. You know something’s broken immediately because your tests fail.
As projects grow you write more tests, and it takes more time to run your tests. It becomes increasingly important to be able to only run one test or the few tests you care about so you can keep moving with the code.
TextMate has some commands to do this (Cmd-R, and Cmd-Shift-R).
Here’s how to do it with vim.
Usually when I’m coding I have Vim open in split screen mode (with rails.vim installed, of course.) I’ll have a spec file on one side, and the application code on the other.
Writing Domain Specific Languages (DSLs) with Ruby
A lot of people say that Ruby is a great languages for writing Domain Specific Languages (DSLs). A DSL is a highly abstracted programming language that gives you a natural and intuitive way to deal with a specific logical domain. They can serve as easy flexible APIs for programmers or enable clients to have control over the way a system deals with their business logic. There are lots of examples of DSLs in the Ruby world. Capistrano, RSpec, Thinking Sphinx, Rails’ Routing, just to name a few.
In this post I’ll look at some of the most common ways to create a DSL in Ruby.
A DSL for Defending Medieval Castles
A new client, Medieval Guards, Inc. specializes in guarding medieval castles and fighting off Barbarian attackers.
My Favorite Bash Substitution Tricks
Here’s a few tricks that I often use on the command line to save time. They take advantage of some variables that the bash shell uses to store various aspects of your history.
Repeating the last command with !!
Sometimes I run a command that requires sudo access, but forget the sudo. This is a great opportunity to use !! which holds the last command you ran.
$ tail /var/log/mail.log
tail: cannot open `/var/log/mail.log' for reading: Permission denied
$ sudo !!
sudo tail /var/log/mail.log
# output of command
Timetrap: Simple Command-Line Time Tracking with Ruby
Who knows where the time goes?
That’s a question that I was asking myself a lot a few months ago. And the results of all that asking was a command line time tracker called Timetrap.
Fix for "random" NoMethodError in Rails' ActiveRecord
I’m a little excited today because some code I wrote was accepted into the Ruby on Rails core.
It’s a patch I wrote to fix a tricky bug I ran into in ActiveRecord which can cause a call to one of your model’s attribute methods to sometimes throw a NoMethodError.
I came across this bug after upgrading a site I help maintain to Rails 2.3. This site allows users to download demos of software. Users have to specify which operating system they want a demo for, and this is stored in a database column named system.
Installing an Interactive Javascript Shell
When you’re brainstorming some ideas for a program or learning a new language its great to have an interactive console to play with. It lets you start typing out code, inspect the return values, and experiment or debug in a really helpful way. You get immediate feedback on what each line of your code is doing. That’s why irb for ruby or script/console for your rails app are such great and useful tools. Once you get used to being able to dive into your application environment and fiddle around you wonder how you ever did without it.
There’s a cross-platform Javascript shell, which is super easy to get up and running. It uses Rhino which is an open-source implementation of JavaScript written entirely in Java.
I’ll show you how to install it on OS X in under 5 minutes.
Most of this should work on Linux (and maybe Windows) too with some minor tweaks.
Instant Search in pure Javascript (almost instantly)
Here’s a really simple method for creating an instant search for a list of items. It lets you quickly filter a long list or table down to the few items you’re looking for.
First here’s a demo.
What’s nice is this only takes a few lines of code (and the magic of jQuery.)
$().ready(function(){
// Instant Search
$('#q').keyup(function(){
$('.search_item').each(function(){
var re = new RegExp($('#q').val(), 'i')
if($(this).children('.search_text')[0].innerHTML.match(re)){
$(this).show();
}else{
$(this).hide();
};
});
});
});
How to load a YAML config object
One of Rails’ selling points is convention over configuration, but most apps do need some configuration. database.yml is a great example.
Here’s some code that loads data into a ruby class from a YAML config file. Just like database.yml the configuration is specific to the rails environment you’re running in.
Resizing Textareas as You Type with Prototype
Resizing Textareas as You Type with jQuery
Here’s some code I’m going to use on every project from now on. It makes all the textarea fields in your forms resize dynamically as the user types. Drop it unobtrusively into any page (running jQuery) and all your textareas expand to fit the text the user has entered.
Smart Pluralize for Rails
Sometimes you want to decide whether or not to pluralize a string based on a quantity. For example, at the bottom of this article should it say 1 comment or 2 comments? It depends on how many comments we have. I was running into this in a lot while pair programming on one project, so Ian Smith-Heisters and I whipped up this simple helper which makes it easy.
class String
def smart_pluralize(num=self)
num.to_i.abs == 1 ? self : pluralize
end
end
