Replace the Hash syntax to the 1.9 syntax for all files, with a regular expression

This morning I got this email from my buddy @elado.

At Gogobot we try to keep everyone on the same coding style as much as we can, follow conventions and readability.

This works best for us since you can be reading code someone else wrote and you won’t feel alienated.

Any way, back to the email, here’s a quote…

Hi guys,

Whoever isn't yet, can you please start using colon notation for hashes instead of arrows?

RIGHT:
render partial: "name", locals: { item: item }


:DEPRECATED =>
render :partial => "name", :locals => { :item => item }

Now, like I said in my reply, I am one of the culprits doing that still, but definitely not alone :-)

So, I replied with a challenge, here’s what I wrote…

Good idea, although I admit I am one of the culprits, it's just embedded into my brain.
I'll do my best...

Challenge for @elado, regular expression to replace those with bash script...

Now, Elad is known as “The Regex king” amongst other nicknames.

After about 10 minutes, I get a Skype ping, Elado is telling me to check my email, this is what I see…

Dir['**/*.rb'].each { |f|
  s = open(f).read
  awesome_rx = /(?<!return)(?<!:)(?<!\w)(\s+):(\w+)\s*=>/
  count = s.scan(awesome_rx).length
  next if count.zero?
  s.gsub!(awesome_rx, '\1\2:')
  puts "#{count} replacements @ #{f}"
  open(f, 'w') { |b| b << s }
}

we did not end up running it on the project, but we definitely plan on running it on files we are working on, so when we commit it will be with the new Syntax.

Thanks to @elado for the awesomeness.

UPDATE: Elad thought it was worth a post as well, so here’s a link to his blog here.

<img src="http://feeds.feedburner.com/~r/KensoDev-en/~4/HMM1r2XDoY8" height="1" width="1"/>
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com