Running the gem doc server at startup on OS/X
Jun/100
I’ve been traveling a fair bit lately, and have occasionally had spotty Internet access. So my access to documentation for gems and whatnot has been limited.
It would be nice to have all of my gem documentation available locally. But I’m lazy. I don’t want to be typing “gem server” all the time.
So here’s what you do. Locate your appropriate gem command with “which gem”; I’m using rvm to run Ruby 1.9.1, so my answer is: /Users/jgn/.rvm/rubies/ruby-1.9.1-p376/bin/gem
Then create a gemserver.plist file like so, replacing the path to my gem with the path to yours:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>localhost.gem.server</string> <key>ProgramArguments</key> <array> <string>/Users/jgn/.rvm/rubies/ruby-1.9.1-p376/bin/gem</string> <string>server</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
Now copy to /Library/LaunchDaemons
The next type you reboot, you’ll have your gem docs at http://localhost:8808/
favicon at ruby-doc.org the wrong size? And what is it?
Jun/100
Here’s the favicon (the image that should show up next to the URL in the browser) for ruby-doc.org:
What IS that? Some kind of stylized “R” + “D”? And why is it the wrong size? Sheesh.
If you must rescue Exception . . .
Oct/090
Sometimes you see Ruby code that rescue an exception at the top of the hierarchy:
rescue Exception => e
If you must do that, how about providing a means to control-C, by putting this in the method with the rescue:
trap("INT") do
puts "Terminating . . . "
return # or maybe exit
end
Faster Net::HTTP for Ruby 1.8.6
Dec/081
We’ve been a bit frustrated at work with Net::HTTP performance (as have so many others) so here’s a monkeypatch for 1.8.6 that combines the buffer size increase in 1.8.7 with Aaron Patterson’s recent tweak to use non-blocking IO (unfortunately, the non-blocking IO patch doesn’t work with HTTPS, which is why we use the buffer size tweak when the @io variable suggests that HTTPS is happening. No guarantee implied, etc.
class Net::BufferedIO #:nodoc: aliasld_rbuf_fill :rbuf_fill def rbuf_fill BUFSIZE = 1024 * 16 # HTTPS can't use the non-blocking strategy below in 1.8.6; so at least # increase buffer size over 1.8.6 default of 1024 if !@io.respond_to? :read_nonblock timeout(@read_timeout) { @rbuf << @io.sysread(BUFSIZE) } return end # non-blocking begin @rbuf << @io.read_nonblock(BUFSIZE) rescue Errno::EWOULDBLOCK if IO.select([@io], nil, nil, @read_timeout) @rbuf << @io.read_nonblock(BUFSIZE) else raise Timeout::TimeoutError end end end end
Teaching Ruby and Ruby on Rails again at Harvard
Aug/080
Once again, I’m pleased to be offering a course on Ruby and Ruby on Rails at Harvard: course; course site.
We’ll try to avoid this anti-pattern:
Ruby: eval, rescue, and Exception
Jun/080
Recently I needed to store bits of Ruby code in the database, and then evaluate them in the context of a particular instance. But one thing I flubbed up was the handling of exceptions. You see, in this situation, if you have bad Ruby code, you will likely raise an exception. My first attempt was, roughly, this code (wrong!):
begin eval s rescue # log the fact that s couldn't be eval'ed end
The catch is that when you use rescue without specify an exception, you are only catching StandardError or one of its subclasses. But eval will raise a SyntaxError (or worse), which is a subclass of ScriptError, which is not a subclass of StandardError. Therefore, you want to rescue an Exception that is further up the hierarchy. In short, better:
begin eval s rescue Exception => e # log the fact that s couldn't be eval'ed end
Nick Sieger provides a post with the Ruby Exception hierarchy, along with some code to generate it dynamically. Too bad this info isn’t in the on-line Pickaxe (http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html).

