Convert Your Entire Project To Ruby 1.9 Or 2.2 Syntax

Posted By Weston Ganger

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:

Article Topic:Software Development - Rails

Date:February 19, 2016