OUR BLOG

Thoughts on development, design and the world we live in.



‘rt’ - Replace Text in Ruby for Rails 3 Upgrade, Charlie Brown

By Curtis Jennings Schofield in Rails, Web Development. Posted on March 17th

https://github.com/robotarmy/rt

In the process of reworking a Rails 2.3.4 application into a Rails 3.0.5 application there were quite a few common changes. Most of the actual work happened in the spec testing layer where RSpec was moving from 1.3 to 2.5 and Webrat was being exchanged in favour of Capybara.

While my apprentice Anita Kuno and myself, Curtis Schofield, were working on this project, I mentioned it was recommended to me by Sarah Allen to take note of process and look at what can be automated. I’ve practiced this myself to an extent - one of the barriers I have encountered in this practice is being told that what you are doing is not necessary by people who are not career programmers. This kind of sentiment can take the wind out of ones sails.

What keeps the wind in sails is turning them to face the correct direction - I have to say that one of these directions (other than working for Captain Recruiter (it is a plug - have no doubt), and working beside Blazing Cloud and Sarah Allen’s Team) is to have an apprentice who is generally intelligent, enthusiastic and willing to explore.

It was Anita’s observation that we were doing many repetitive tasks - she asked if there was a way to automate that process. I responded by saying I had written something similar years back. I didn’t have the code anymore. However, I knew that we would be able to get some small practical tool going.

This tool is ‘rt’. A small test-driven program for asking an operator if they would like to run a regular expression on a line.

‘rt’ assumes that you are comfortable in writing (or copying) lines of YAML and are familiar with how to compose regular expressions. Regular expressions can be difficult to master or grasp the fundamentals of - many people will tell you that if you attempt to solve a problem with regular expressions - you will have 2 problems. In fact - they can be very useful and advantageous - as long as you do not expect them to be a complete substitute for a formal parser. I have used regular expressions for 10 years and they can be excellent when used in the correct place.

You will notice when running ‘rt’ that each of your regular expressions are executed on the every line in the file - matches are presented to you and you will be given a choice to accept the transformation or deny it (the ‘return/enter’ key will deny the change).

If you would like to use the actual file that we used to transform the code from RSpec 1 to 2, Webrat to Capybara and a few things around Rails 2 to rails 3. You can find this file in the repository

You will also see that we renamed an entire object ‘user’ to ‘person’ and all the plural elements of such.

Remember Tron, Users are People.

  • here is an example of taking out debugger statements
  • here is an example of converting - with a set of steps ‘users to people’ and ‘user to person’
  • Webrat existed in a couple different incarnations - depending on the implementer - in this one I use references to match expressions

so how does one run ‘rt’?

‘rt’ can take a file or a directory - it looks for a .snoopy file at startup

This .snoopy file is you YAML collection of regular expression key value pairs

example .snoopy

Any questions? email : [email protected]

:wq 

Post a Comment

Your email is never shared. Required fields are marked *

*
*