Archive for January, 2010

How to Buy Books

Friday, January 15th, 2010

I only give free advertising to truly exceptional companies. BookFinder.com is one of them. I have spent wasted countless hours looking for the best price on books from the usual suspects: Amazon, eBay, and half.com. No longer! BookFinder.com quickly and easily locates books from a multitude of sellers and includes the shipping cost in the price. And if you have trouble with the site, their customer service is fast and friendly. Two thumbs up for BookFinder.com!

Convert Inches to Feet and Inches

Wednesday, January 6th, 2010

I love simple problems, such as fill a drop-down with a range of feet and inches.

One way to do it is to hand-code a <select> element and fill it with hand-coded <option> elements, but what’s the fun in that?

It’s more fun to iterate over an array (range) of integers representing inches, and do the math to convert those inches to feet and inches. To get feet, use the floor function, which truncates all decimal places without rounding. To get inches, use the modulus operator (the % operator) which returns the remainder. 36 is 3′0″ and 74 is 6′2″.

The math is the same but (as John Madden would say, if he were a programmer) the syntax varies by language… Here’s how you do it in Ruby:

<%= select :person,
       :height,
       (36..95).collect{|i|["#{(i/12).floor}' #{i%12}\"",i]},
       {:selected=>@person.height} %>

And here’s what you end up with:

And unformatted:

select :person, :height, (36..95).collect{|i|["#{(i/12).floor}' #{i%12}\"",i]}, {:selected=>@person.height}

Loop, svn log, awk, cat, egrep echo Example

Tuesday, January 5th, 2010

So, I just checked in some files and I realized “doh! I didn’t check to see if there’s any debug trash in there!” My debug trash is fairly easy to detect: I only use raise or puts in Ruby and alert in JavaScript for basic “hello world” type things when debugging some particular issue.

Trouble is, sometimes I forget to remove that trash code before checking it in. To prevent big problems, I refrain from using the Ruby raise command and instead use puts which is fairly harmless. Anyway, I wrote a script to 1) ask SVN for the list of files modified by my checkin and 2) use awk to figure out the file names and, within a loop, 3) use cat to pass the contents of each file to 4) grep / egrep to look for alert, raise, and/or puts statements within the files. So now I have an example of a handful of common Linux commands that I can post here for future reference.

for l in `svn log -r 298 -v | egrep M | awk '{print $2}'`;
do
echo ${l};
cat .${l} |
egrep 'alert|puts|raise';
done

Which spat out:


/app/models/some_model.rb
/app/models/some_other_model.rb
/app/models/foo_model.rb
/app/controllers/home_controller.rb
    puts "hello world"
/app/controllers/foo_controller.rb
    #puts "hello world"
/app/controllers/other_controller.rb
    puts "hello world" unless some_condition

And I found three instances of debug trash… One commented out, one from a previous check-in, and one that I need to remove!

And on one line, without formatting:

for l in `svn log -r 298 -v | egrep M | awk ’{print $2}’`; do echo ${l};cat .${l} | egrep ’alert|puts|raise’;done