Main | iOS development with Vim »

Refactoring Rails With sed -- Class Method Renaming

Something that you'll ocassionally hear from new Rails developers (especially if they are coming from an IDE-centric world) is that they miss having refactoring tools. Well, it's never going to be as easy to automate refactoring in a dynamic language as it is in a static language. All of that compile time info in static languages is very useful for refactoring and in Ruby we just don't have that to work from. That said, as terminal dwellers there is a lot of power that we can gain from leveraging UNIX tools. Here is one way in which I have learned to take advantage of BASH and sed, to rename a class method in a rails app. Here I am renaming a class method called 'omc_data_service' to 'service'. I'm all about pulling this stuff off as one liners right in the terminal. It seems a bit crazy at first, but after you do it a few times you can come up with something like this in about 20 seconds.

 

 

It's pretty straight forward really

 

 This is just saying "Loop over all the Ruby files starting from the current dir and recursing"

 

 Print the file name


This is the real logic. Sed is a line based editor that we are using here to make a regex substitution.
The "-i" flag means "Do the subsititution inline, rather than to stdout".
The "-e" flag means "the following string is a sed expression".
The first regex "/[:.]omc_data_service/" is saying "Only apply this to lines that contain the method name". This is only needed because we don't want sed to rewrite lines when there is no change made. Without the first regex the result would be the same, except every file would have it's 'last modified' date changed.
Anyway, we are looking for either a starting '.' for calling and declaring the class method, or a starting ':' for when we are passing the method name as a symbol to Flexmock. The '\([:.]\)' means, "Remember if it started with a '.' or ':'". The "\1" says "Since you rememebered if it had a '.' or ':', put that here". Finally the "/g" takes care of all occurances on a line rather than just the first.


Hope this was helpful.

 

 

 

Reader Comments (11)

You don't need for, echo, do/while:

find . -name \*.rb -print -exec sed -i -e "/[:.]omc_data_service/ s/\([:.]\)omc_data_service/\1service/g" \{} \;

March 29, 2011 | Unregistered CommenterJonas Bülow

..and you don't need to repeat the regexp:

find . -name \*.rb -print -exec sed -i "/[:.]omc_data_service/ s//\1service/g" \{} \;

alt:


find . -name \*.rb -print -exec sed -i "s/[:.]omc_data_service/\1service/g" \{} \;

March 29, 2011 | Unregistered CommenterJonas Bülow

May I suggest piping into xargs instead of the for loop?

find . -name \*.rb -print0 | xargs -0 sed --i -e "/[:.]omc_data_service/ s/\([:.]\)omc_data_service/\1service/g"

March 29, 2011 | Unregistered CommenterNicholas Fine

..but make sure you capture [:.], i.e use the alternative version:

find . -name \*.rb -print -exec sed -i "s/\([:.]\)omc_data_service/\1service/g" \{} \;

Sorry, about that.

March 29, 2011 | Unregistered CommenterJonas Bülow

If you fancy command line, chances are vim is your editor of choice. In which case you might find EasyGrep useful. It does all that and more.

March 29, 2011 | Unregistered Commenterartemave

This is a good article,thank you for your share,I like it very much.Our products are superior in quality and moderate in price and are sure to be saleable in your market,by virtue of this superior quality, this product is often sold out in many areas. This new product is really much better than the previous one. You have to try it to believe it.

-Chopard Miglia GMT Swiss made watches

I'm so glad you like the blog. Faded Elegance is so beautiful...you are right. This blog has been so much fun to write. I just love it!

-Bally Leather Shoes for less

freelance writer

July 20, 2011 | Unregistered CommenterCorina31Mathews

jordan fusion new releases
jordan fusion wristbands
Air Jordan 6 Max Fusion
Air Jordan 11 Max Fusion
Air Jordan Fusion 4
LV Handbag Outlet Sale
jordan fusions new releases
jordan fusion ones
real louis vuitton handbags
jordan fusion obama
jordan fusion royal
Authentic Louis Vuitton
jordan fusion lifestyle
jordan fusion logo
air jordan fusion 12
jordan fusion june 14
Jordan Fusion 5
jordan fusion nike
jordan fusion two
air jordan fusion myspace layouts
air jordan fusion retro basketball shoes
new jordan fusion 09
jordan 12 fusion
jordan fusion nike shoes
Air Jordan Fusion 6
new jordan fusion 3
Jordan Fusion 3
nike air jordan fusion kids
jordan fusion retro 8
LV Handbag Outlet Sale
jordan fusions lime green
jordan fusion releases
jordan fusion sale
louis vuitton hats
jordan fusion v sneaker
jordan fusion premier
custom jordan fusion kicks
air jordan fusion one
jordan fusion max
jordans for sale
jordan fusion 15
jordan fusion v cactus
jordan fusion shoes cheap
jordan fusions womens
jordan fusion 3 premier
jordan fusion og jacket
jordan fusion 20 low
jordan fusion new release
jordan fusion wholesale
jordan fusion 25
jordan fusion viii
cheap jordan sneakers
cheap jordan fusion shoes
air jordan fusion websites
jordan fusion paypal
air jordan fusion xi
air jordan fusion wholesale
jordan fusion pe
Air Jordan Fusion 9
jordan fusion sneakers
air jordan fusion 3
jordan fusion list
jordan fusions premier
jordan fusion myspace layouts
nike jordan fusion two
Jordan Fusion 8
jordan fusion white
jordan fusions newest ones
official jordan fusion website
air jordan fusion new
jordan fusion vi
Jordan 2011 Max Fusion
air jordan fusion mid
jordan fusion v 5
jordan fusions white blue trim rainbow sole
jordan fusion xl
Louis Vuitton Men Low Shoes
brown jordan fusion outdoor furniture
air jordan fusion nine
air jordan fusion playoff
jordan fusion xxiii 23
air jordan fusion xiii
authentic LV Scarves
jordan fusion shirts
jordan fusion olive/orange
jordan fusion v wine
jordans fusion mid 13
Air Jordan Fusion 12
jordan fusion release dates 2011
jordan fusion 12 sneaker
jordan fusion youth
mens louis vuitton sunglasses
jordan fusion 20
jordan fusion three
jordan fusion 3 varsity
jordan fusion low top
jordan fusion
jordan fusion with air force ones
Jordan 12 Max Fusion
jordan fusion 07
jordan fusion release dates
jordan fusion layouts
Air Jordan 13 Max Fusion
jordan fusion rainbow
air jordan fusion jacket
air force jordan fusion new
jordan fusion xx for women
jordan fusion myspace
Louis Vuitton Scarves
jordan fusion warehouse
jordan fusion titanium
air jordan fusion vi
louis vuitton tie clip
jordan 5 fusion
jordan fusions 06
jordan fusion pink
Air Jordan Fusion 20
wishesg 20 jordan fusion men shoes info
air jordan fusion t shirt
jordan fusion kicks
jordan fusion quarter sock
jordans fusion mid 3
jordan fusion shoes sneaker
Jordan 3 Max Fusion
jordan fusion real
jordan fusion xii
authentic jordan fusion v
Men T-Shirt
jordan fusion prices
jordan fusion 10
jordan fusion mix
jordan fusion 11
air jordan fusion 3 shoes
Air Jordan 9 Max Fusion
jordan fusion tags
jordan fusion 2009
jordan fusion red
air jordan fusion release dates
jordan fusion 24
jordan fusion peel
Louis Vuitton Ties
jordan fusion 22
air jordan fusion 23
jordan fusion low
jordan fusion retro 5
jordan fusion 25 & jordan 6 men shoes
jordan fusion 05
jordan fusion on sale
Jordan Fusion 13
jordan fusion release dates 2010
jordan fusion new
air jordan fusion youth
air jordan fusion kids
jordan fusion 09
jordan fusion plaid

For this statement, Jochen Zeitz argued that a considerable part of the product had being through wind and rain to counter the trend of fail for decades yksdhg yksdhg - Mulberry Alexa Outlet.

November 13, 2011 | Unregistered Commenterpthnnb pthnnb

Whatever style you choose, Jordan Retro Shoes has a shoe that is custom made for you and with so many styles to choose from,
you can find the perfect Cheap Jordan Shoes.

December 31, 2011 | Unregistered CommenterJordan Retro

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>