Torsten Bühl
Software Engineer & Founder

Hi, I'm Torsten. I am the founder of Foodlane, Company Signal, and Exceptiontrap (acquired by Scout APM). I strive to build simple and beautiful software products that people love to use. Learn more about them here

Transforming Ruby's hash rocket to the new syntax

Torsten Bühl Profile

When I work in projects that started before Ruby 1.9, I don’t like the mix of the old hash rocket style hashes, and the new syntax.

If you want to transform the old style to the new one, this simple regular expression will save you some time. Just use the following string in your “Find” or “Find in Project” prompt in Textmate.

# Find/replace regex for Texmate
# Find:
:([a-z0-9_]+) =>

# Replace:
$1:

If Vim is your editor of choice, just change $1 to \1.

This will transform all hashes from the old format to the new one.

# Before
:foo => :bar
:foo => "bar"

# After
foo: :bar
foo: "bar"

# But doesn't affect
"foo" => "bar"

The regular expression uses only lowercase letters, so that it doesn’t find namespaced classes as the key. Also, uppercase letters in symbols are pretty rare (but allowed).

# for example in Rails's routes.rb
mount Sidekiq::Web => '/your-path-here'