Rails

How To Convert Your Entire Project To Ruby 1.9 Or 2.2 Syntax

Posted by Weston Ganger on February 19, 2016

I absolutely loath the old ruby syntax, theres just too many characters.

Here I will show two examples in each relevant version of ruby


# Ruby 1.8
:class => 'my-class'
'data-confirm' => 'Are you sure?'

# Ruby 1.9
class: 'my-class'
'data-confirm' => 'Are you sure?' # it doesnt handle hypens

# Ruby 2.2
class: 'my-class'
'data-confirm': 'Are you sure?'

The first and most important find and replace regex I would like to cover is switching to Ruby 1.9 Syntax. Run this command in your apps root folder.


find . -type f \(-name "*.rb" -o -name "*.erb" -o "*.slim" -o "*.haml" -o "*.prawn") -exec ruby -i -p -e "gsub(/([^:]):(\w+)\s*=>/, '\1\2:')" {} \;

Next your may also want to convert any of the leftover hypenated keys to Ruby 2.2 syntax


find . -type f \(-name "*.rb" -o -name "*.erb" -o "*.slim" -o "*.haml" -o "*.prawn") -exec ruby -p -e "gsub(/([^:]):((\w|-)+)\s*=>/, '\1\'\2\':')" {} \;


Related External Links:


Posted in Rails and Tagged with ruby 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts