When working on old Ruby code, you’ll see the old (<= 1.9) hash syntax:
However, often in new projects you’ll want to use the new:
I try to convert to the new, and thus, when editing old files, I tend to replace the old with the new syntax. This is a tiresome job, and therefore I wrote a Regex to do it:
It would be nice, if you didn’t have to put this into SublimeText 2 every time you wanted to switch the old for the new syntax. I found RegReplace for SublimeText 2, which will allow you to create Regex replacements. It will also allow you to create SublimeText commands which can be bound to keyboard shortcuts (hurray!).
To get a shortcut to replace the old syntax with the new do the following:
- Edit your
- Add a shortcut to the default
Now you can just press “⌘+⇧+x” and replace the old with the new.
There’s a couple of shortcomings in the above regex. It will match these inside strings! Really doesn’t matter for my use case.
It will also not retain the indention. That is, the example I gave at the top, will not work as shown. Rather, this:
will become this:
That really will not do, so we change the regex to:
This will retain the indention and make sure that we will get:
The RegReplacer regex will now be: