Software Developer and Founder Torsten Bühl

I am the founder of Company Signal, Exceptiontrap and Positron – SaaS products with a focus on simple usage and a beautiful & clean user-interface. Learn more about them here. You can also find some articles about software development (especially Ruby related) below.

 

Transforming Ruby's hashrocket (to the new syntax)

21 Oct 2013

When working in projects which started before Ruby 1.9, I don't like the mix of the old hashrocket style hashes and the new one.

So when you want to transform the hashrocket syntax to the new one in your project, this simple regex 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 where possible.

# transforms
:foo => :bar
:foo => "bar"
# to
foo: :bar
foo: "bar"
# but doesn't affect
"foo" => "bar"

The regex uses only lowercase letters so that it doesn't find namespaced classes as the key. Plus uppercase letters in symbols are pretty rare (but allowed).

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