Ruby 1.9 hash syntax isn’t new ({ like: “this” }) and some projects are still using the old way. So how do you fix all your project to match the “new” syntax?

Basically you’re looking for:

{ "something" => "like this", :or_a_symbol=> "" }

We’ll need to find all ruby files and search for hashes using old format, then we’ll subtitute all occurrences:

$ /bin/find . -type f -iname '*.rb' -exec grep -il '=>' {} \; | while read file ; do /usr/bin/perl -p -i.bak -e 's/:([\w\d_]+)(?:\s*)=>/$1:/g' "$file" ; done

And that’s it. Thanks to this post for the regex.

Update: we’ll break rescue lines when the command above is executed, so we’ll need to replace hashes only for non-rescue lines, e.g:

~ $ /bin/find . -type f -iname '*.rb' -exec grep -il '=>' {} \; | while read file ; do /usr/bin/perl -p -i.bak -e 's/:([\w\d_]+)(?:\s*)=>/$1:/g if $_ !~ /rescue/' "$file" ; done