Categories
Uncategorized

Sublime Text: Convert Ruby 1.8 to 1.9 style hash syntax

How to automatically replace and convert Ruby 1.8 to 1.9 style hash syntax using Sublime Text without any plugins or gems.

Ruby 1.9 and 2.0 has a new hash syntax. In Ruby 1.8 it was:

hash = { :one => “first value”, :two => “second value” }
In Ruby 1.9 and 2.0, this can now be written as:

hash = { one: “first value”, two: “second value” }
Saving three characters per key.

If you’re using Sublime Text, it’s really easy to do this conversion automatically.

Here’s how to do it – use it at your own risk, and take a backup first:

Go to Find → Find in Files…
Enable Regular expressions – make sure the .* button is activated
In the Find box: :([a-z_\d]+) =>
In the Replace box: \1:
Click the Replace button. This will replace matches in all of your files in Sublime Text. It will also go through non-Ruby files so it might be a good idea to set a Where file pattern to *.rb or *.html.erb. As I said, use at your own risk, and take a backup first.