Log In

Matt Briggs

"Not all code needs to be a factory, some of it can just be origami." - _why, the lucky stiff

Ruby in daily scripting

ruby
by Matt Briggs on 02/18/10

Ran into an issue where merges were getting corrupted for some reason, and adding a random garbage character to the end of some files. Whipping up a script that verifies the files is obviously the way to go in situations like that, and what I have been doing for years is using perl as a sort of glue language for unix command line utilities. As I get more comfortable with ruby, I find it is becoming more and more my weapon of choice in these situations.

There are three things that make a good "glue" language: great regex syntax, great file manipulation APIs, and great command line integration. Those three things were always my favorite things about perl, and thankfully, they have been copied over pretty much verbatim into ruby.

In this situation, I need to do a recursive search through a directory tree. Find does that in a pretty straightforward manner. Regex is great in ruby, using the =~ operator and standard regex syntax /pattern/, it will return the number of matches, or nil for nothing. Last piece is command line integration, anything inside of back ticks (`) will execute on the commandline, and then evaluate as the return value.

So, what we want to do is walk the source tree, look for source files (which should all end with >, since we are talking about xml-ish files), grab the last line, and make sure it ends with something valid (with a bit of experimentation, I found that for my site that meant >, space, tab, or newline)

What I ended up with is this

require 'find'

puts 'starting search....'
puts '-----'
Find.find(ARGV[0]) do |path| # walk the site tree
  Find.prune if File.basename(path) == '.svn' # don't go into svn folders

  if path =~ /(aspx|ascx|resx)$/ # only test aspx, ascx, or resx files
    last = `tail -1 "#{path}"` # call out to tail
    if last =~ /[^> \t\n]$/ # if it is not one of the safe characters
      puts path
      puts last
      puts '-----'
    end
  end
end
puts 'done.'

Very fast to write, very straight forward to read.

my code blog.

what I am reading

Sidebar_clean_code

the people I follow

  • 24 ways
  • ABtests.com - Learn. Share. Improve your conversions today.
  • Ajaxian » Front Page
  • Alex Young
  • BEST IN CLASS
  • briancarper.net (λ)
  • Carbonica Blog Feed
  • Catalog Living
  • Clients From Hell
  • Clojure/core Blog
  • code is code
  • Coding Horror
  • CSS-Tricks
  • Daily Vim: Text Editor Tips, Tricks, Tutorials, and HOWTOs
  • David Chelimsky
  • dean.edwards.name/weblog
  • DHTML Kitchen News
  • disclojure: all things clojure
  • Edge Rails.info
  • End of Line
  • English - AkitaOnRails.com
  • Err the Blog
  • Evil Monkey Labs
  • Extra Cheese
  • Extra Cheese
  • For A Beautiful Web
  • Francis Hwang's site
  • Free Ruby and Rails Screencasts
  • Giles Bowkett
  • Hacker News
  • has_many :bugs, :through => :rails
  • Higgins for President
  • HTML5 Doctor
  • Information Is Beautiful
  • It's an all-you-can-leet buffet !
  • Jay Fields' Thoughts
  • JGUIMONT>COM
  • John Barnette
  • John Resig
  • K. Scott Allen
  • Katz Got Your Tongue?
  • Kirby's Dreamland
  • Kotaku
  • Kotka
  • Lambda the Ultimate - Programming Languages Weblog
  • Lazycoder
  • Loud Thinking by David Heinemeier Hansson
  • LukeW | Writings on Digital Product Strategy and Design
  • mir.aculo.us
  • MongoTips by John Nunemaker
  • Moonbase
  • No Strings Attached
  • Nuby on Rails
  • Official jQuery Blog
  • ones zeros majors and minors
  • opensoul.org by Brandon Keepers
  • Painfully Obvious
  • Painfully Obvious
  • Particletree
  • Paul Irish
  • Perfection kills
  • Plataforma Tecnologia Blog » English
  • Rails on PostgreSQL :
  • Railscasts
  • RedFlagDeals.com - Latest Deals
  • Relaselog | RLSLOG.net
  • remy sharp's b:log
  • Riding Rails - home
  • RightJS News
  • rmurphey
  • Room 101
  • Rubinius Blog
  • Ruby Best Practices
  • Ruby Inside
  • Ruby Quicktips
  • Ruby treats women as objects
  • RubyFlow
  • Signal vs. Noise
  • Slash7 with Amy Hoy - Home
  • Smashing Magazine Feed
  • Snail in a Turtleneck
  • Software Craftsmanship – Katas
  • St. on IT
  • Stevey's Blog Rants
  • Technomancy
  • Tender Lovemaking
  • Test Obsessed
  • Zed Shaw
  • The CSS Ninja
  • The GitHub Blog
  • The MongoDB NoSQL Database Blog
  • The Napkin ~ A Blog By Highgroove Studios
  • The UX Booth
  • The Word of Notch
  • the { buckblogs :here } - Home
  • Thoughts From Eric
  • Uncle Bob's Blog
  • VIM Tips Blog
  • Virtuous Code
  • Web Designer Wall - Design Trends and Tutorials
  • Wow! eBook - Great ebook, great site!
  • #<Mongoid::Criteria:0xb2d868c>
profile for Matt Briggs at Stack Overflow
Feed
atom 1.0

mattcode.net stack

Rightjs
Rails
Mongo
Dropbox