Ruby $1 vs \1

Posted on May 25, 2006

I was attempting to take some unordered SQL values and order them today with a single line of ruby. Essentially, the sql values look like (1, "value1"), (2, "value2"), and so on. I started out with this, but I couldn’t for the life of me figure out why the gsub statement wouldn’t work.

INCORRECT: ruby -e " y = []; STDIN.readlines.each { |x| if x=~/^\(/; y[x.gsub(/\(([0-9]+),.*/, $1.to_s).to_i]=x; end }; y.each { |z| puts z if ! z.nil?; } " < unordered.sql > ordered.sql

The weird thing is, if I take the if x=~ /^\(/; statement out, the code works. So, someone in #ruby-lang on Freenode reminded me that I need to use '\1' instead of $1.to_s in this case. I ran into this once before, however, I’m still not 100% sure on what the exact rules are for the syntax. So, this was the correct way to do it.

CORRECT: ruby -e " y = []; STDIN.readlines.each { |x| if x=~/^\(/; y[x.gsub(/\(([0-9]+),.*/, '\1').to_i]=x; end }; y.each { |z| puts z if ! z.nil?; } " < unordered.sql > ordered.sql

Pathmunge

Posted on May 11, 2006

This one came from reading RHEL4U3′s default /etc/profile today. Before you call me a nerd and go back to your so called life, hear me out on this. So, the good folks in Raleigh decided to add a function called pathmunge to their default profile. This function just allows you to type stuff like pathmunge ~/bin after to add a directory to your search path. Most self-respecting nerds probably sorted this out in 1987, however, I’m an idiot and continue to do things the old fashioned way until I’m bludgeoned with the technology stick. Unfortunately, pathmunge doesn’t get added to the default shell environment for all users. So, I just added it to /etc/bashrc on my machines today. Here is how to implement pathmunge in all of its glory…


pathmunge () {
  if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
    if [ "$2" = "after" ] ; then
      PATH=$PATH:$1
    else
      PATH=$1:$PATH
    fi
  fi
}

Feel free to throw this into your ~/.bashrc if you haven’t implemented it already.

The Way I See It…

Posted on May 10, 2006

When ordering a latte, the barrista should assume “hot” unless otherwise noted. Iced lattes are for the weak of mind and spirit who are unable to understand the beauty of hot beverages. Thus, they should be made to suffer by being forced to utter this heinous adjective every time they commit the sin. The iced latte people are almost as bad as those who order breakfast tacos without eggs. You know who you are.

When the Levee Breaks

Posted on May 09, 2006

I was working through some exercises from Stanton Moore’s “Take It To The Street” book last night. One of them was the initial groove from Led Zeppelin’s “When the Levee Breaks” off IV. As with any organic groove like this, there are subtle nuances that are very much overlooked by most of the drumming community. In this case, John Bonham was accenting certain hi-hat beats during most of the song. The hat trickery would have gone unnoticed had Stanton not been such a perceptive lad. Even the drum tab that I picked off the Internet for my example missed them. These beats are tough to accent because they occur in a quite unnatural position between syncopated and not. Don’t mistake this for drumming erudition either as these accents punch through the extreme reverb that was applied to his kit to make the groove feel so tight. Here is the ASCII-art drum tab with the accented hi-hats beats highlighted for all the world to see.