Archive for the ‘ruby’ Category

zsh: Scratching a geeky itch

Saturday, April 4th, 2009

I’ll apol­o­gize right out of the gate for the resem­blance this post bears to one Rafe Col­burn made today. We sit across from each other at work, and we’re both div­ing into zsh at the moment, so cross-pollination was prob­a­bly inevitable.

The other day, I switched my shell from bash to zsh. Just like Rafe, I read the Fried CPU post about zsh, and was con­vinced to give it a whirl. It’s been in the back of my mind for a while now, since I’ve notice that a lot of peo­ple whom I respect (Ryan Bates, for one) are using it, but the con­cise list of really pow­er­ful fea­tures in the Fried CPU arti­cle was the tip­ping point. I’ll admit that, never hav­ing switched shells before, I thought it was going to be a heck of a lot more com­pli­cated than just typ­ing chsh and replac­ing “/bin/bash” with “/bin/zsh” in the con­fig file that pops up. Had I known it would be that easy, I would’ve tried it a lot sooner.

At first, I tried copy­ing huge chunks out of the zshrc & other zsh con­fig files that Joe Fer­ris main­tains in a repo at Github, but even­tu­ally decided I’d get more out of this exper­i­ment if I took more bite-sized chunks out of other peo­ples con­fig­u­ra­tions, and only added them to my own when I under­stood what they were doing.

That’s when things got out of hand.

When I was copy­ing bits of my old bash con­figs over to my zshrc, I noticed how dis­or­ga­nized all of my dot­files were–not just the shell ones, but my irbrc, and vim & emacs con­figs, etc., etc., etc. So I basi­cally decided to scrap all of them and take the same approach to them as I was tak­ing with my shell con­figs. So basi­cally, I’m start­ing over with Unix, and slowly rebuild­ing what I’m hop­ing will be a super-organized and super-optimized envi­ron­ment for future work and play.

If you’re inter­ested in see­ing how things develop, you can fol­low along with my dot­files repo on Github. But what I’m sure will be infi­nitely more inter­est­ing will be to start on your own adven­ture in Unix con­fig­u­ra­tion. If you go down that road, here are some of the resources I’ve used in rebuild­ing my environment:

  • Joe Ferris’s config_files repo: I men­tioned this before, but it’s worth men­tion­ing again, due to its being awesome.
  • dotfiles.org: User-conributed dot­files for just about every *nix-based util­ity that uses a text-based con­fig­u­ra­tion file.
  • irb & script/console tips: Obvi­ously these are only use­ful to Ruby­ists, but if you swing that way, the­ses are well worth check­ing out. Ever since I first saw the SQL gen­er­ated by an ActiveRe­cord query show up in someone’s script/console, I’ve cov­eted that func­tion­al­ity. Dan Croak shows how it’s done.
  • Dr. Nic also has some great irb tips
  • Finally, I cribbed a git-aware prompt from this screen­cast. A very neat trick.

So yeah, it’s been really fun get­ting back to basics with my Unix con­figs. That said, I have had one small hic­cup in this process. After going through all of this with my OSX ter­mi­nal, I went to switch my shiny new Ubuntu JeOS VM’s shell over to zsh, but found that after doing so, my delete key wasn’t behav­ing like a back­space as it had been doing when I was using bash. Appar­ently this is a known issue when using zsh or screen over ssh. I still haven’t found a good workaround for this, and so am still using bash in Ubuntu.

All in all, switch­ing from bash to zsh has been an extremely reward­ing expe­ri­ence, and def­i­nitely one I’d rec­om­mend to any­one look­ing to change up their rou­tine and learn some­thing new and useful.

Install Mongrel & Hpricot Under Ubuntu

Monday, March 24th, 2008

When­ever I do a fresh install of Ubuntu, and I’m set­ting up Ruby, Rails, I always run into the same prob­lem with a hand­ful of gems (such as Mon­grel & Hpri­cot). This is how things usu­ally go down:

matt@thinkpad:~$ sudo gem install hpricot
Building native extensions.  This could take a while...
ERROR:  Error installing hpricot:
ERROR: Failed to build gem native extension.
/usr/bin/ruby1.8 extconf.rb install hpricot
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:1
Gem files will remain installed in
/usr/lib/ruby/gems/1.8/gems/hpricot-0.6 for inspection.
Results logged to
/usr/lib/ruby/gems/1.8/gems/hpricot-0.6/ext/hpricot_scan/gem_make.out

Now what? The main prob­lem here is that Hpri­cot and Mon­grel both con­tain some C code that needs to be com­piled. In order to fix this, you’ll need to install the Ruby & C devel­op­ment libraries for Ubuntu. To do that, just fire up Ter­mi­nal and enter:

sudo apt-get install ruby1.8-dev linux-libc-dev libc6-dev

You should now be able to install Hpri­cot, Mon­grel, or any other gem that requires you to build some C. It’s that easy!

Stupid Ruby Problem

Wednesday, January 9th, 2008

Like I said, I’m work­ing on get­ting my Ruby chops up to snuff. I thought it might be fun to do that by port­ing all of the code in Joseph Adler’s fan­tas­tic Base­ball Hacks from Perl to Ruby.

I’m com­ing back to this project after let­ting it go for a while, and sure enough, the prob­lem I was hav­ing when I put it down is still vex­ing me.

One script is intended to grab all of the play-by-play data from retrosheet.org. The files all have urls like this:


http://www.retrosheet.org/1957/1957al.zip

http://www.retrosheet.org/1957/1957nl.zip

http://www.retrosheet.org/1957/1957ml.zip

The snip­pet below is a sim­pli­fied ver­sion of the code I’m using to build the urls. It’s intended to print the urls of each zip file for each league from 1957 to 2006:

year = 1957
leagues = ['al', 'nl', 'ml']leagues.each do |league|
  while year <= 2006
    url = "http://www.retrosheet.org/#{year}/#{year}#{league}.zip"
    puts url
    year += 1
  end
end

How­ever, it doesn’t do that. It only gen­er­ates the urls for the AL files, and I can’t get my brain around why.

I’ll be sure to add the solu­tion when I fig­ure it out, but for now I wanted to just doc­u­ment some of the strug­gles of a Ruby n00b.