kwno0w
Last Updated: February 25, 2016
·
4.346K
· nicolaslazartekaqui
Img 3566

Vim - Regex to change hash rockets to new hash syntax in Ruby

Change this way

:param => 'value'

for this way

param: 'value'

open .rb file in vim and do

%s/:\([^=,'"]*\) =>/\1:/g

if you want use the interactive mode, add c in the end of command

%s/:\([^=,'"]*\) =>/\1:/gc

case you want the inverse, try

:%s/\(\w*\): \([':]\)/:\1 => \2/gc
Say Thanks
Respond

8 Responses
Add your response

9705
63f35d9e50dfd73281126b051a51668a

Technically, these aren't arguments. They're values for hash keys. Also, this isn't Rails specific. It's Ruby. Rails just happens to use these hashes as the data type for its params. The more appropriate title would be, "Vim regex to change hash rockets to new hash syntax in Ruby."

That said, this is helpful. Thanks.

over 1 year ago ·
9706
Img 3566

@thenickcox thkz, i agree with this title is so much clearer :)

over 1 year ago ·
9715
7bd666137745a01b596f05969475428d

What about {:'a' => 'b'} ? ;)

over 1 year ago ·
9729
Img 3566

@woto {:'a' => 'b'} === {:a => 'b'} === {a: 'b'}

over 1 year ago ·
9731
7bd666137745a01b596f05969475428d

I mean {:'a' => 'b'} {'a': 'b'} - invalid

Also there is situation when it can't be rewritten in this style, for example {:'!@#' => 'b'}. That's why its command can't be used in batch replacements.

over 1 year ago ·
9732
Img 3566

@woto i understood, i updated the tip to ignore this cases
thkz (:

over 1 year ago ·
12602
5540796cef3803aa6a9a2bcb62983858

Is there a regexp syntax to do just the inverse?

over 1 year ago ·
12606
Img 3566

@kikito I think this solves

:%s/\(\w*\): \([':]\)/:\1 => \2/gc
over 1 year ago ·