Quantcast
Channel: Lasse Bunk's weblog
Viewing all articles
Browse latest Browse all 14

Sublime Text: Convert Ruby 1.8 to 1.9 style hash syntax

$
0
0

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:

  1. Go to FindFind in Files
  2. Enable Regular expressions – make sure the .* button is activated
  3. In the Find box: :([a-z_\d]+) =>
  4. In the Replace box: \1:
  5. 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.
  6. Enjoy.


Viewing all articles
Browse latest Browse all 14

Trending Articles