Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Software Developer at Hedgeye. Previously, I was a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize NewHaven.rb. I do development with Ruby, JavaScript, SQL, HTML, and CSS. I have an amazing fiancée named Danielle Smith.

Filtering for the Ruby category. Clear

throw/catch vs. raise/rescue

by Ben

Just a piece of Ruby trivia: Ruby has both throw/catch as well as raise/rescue. Most newbie Rubyists don’t know this. There’s a section of the Pickaxe book that discusses them both.

Essentially, rescue is used for exceptions and error control while catch is used for symbols and flow control. The idea is that you shouldn’t use exceptions to change the flow of your program, but rather to handle errors.

“Ruby, White and Blue Hackfest” tomorrow!

by Ben

I’ve had the great pleasure of working with some other local Ruby developers to reignite NewHaven.rb over the last few weeks. Our second official event is a “Ruby, White, and Blue Hackfest” tomorrow, July 1st. It’s going to run from 6-9 pm at Blue State (the Wall St location). Everyone is welcome — don’t worry about skill level! You bring a laptop, we bring the community.

Right now, the plan is to get together and work on a few projects that group members are interested in, including our upcoming website. I’d also like to talk about what we should do for our next event.

If you’re planning on coming, please RSVP.

New on GitHub: utilities

by Ben

I spent some time over the last few weeks pulling together lots of command line tools that I’ve written over the last few years. I’ve shared them on GitHub.

Some fill in gaps that I wish *nix systems would have by default (such as prune vs uniq or reverse vs rev). Others just script things that I do commonly or are just tedious to do (such as backups2git, github-init, timestamp, latest-migration-path, and std-timestamps). Some are just there for fun (such as is-computer-on). Most of them are written in Ruby, but some are plain old Bash scripts. Lots of the Ruby scripts make heavy use of ARGF, which is awesome for writting shell scripts if you’ve never used it.

I also spent some time documenting (and remembering) how they worked. (Most of it was just shuffling comments around.) Almost every command has a --help option that prints usage information and a short synopsis now. I hope you find them useful!

See them on GitHub

Throw exception when re-assigning a constant in Ruby?

by Ben

This has bothered me for a while, so I made a question on StackOverflow:

I’ve long been aware that “constants” in Ruby (i.e., variable names that are capitalized) aren’t really constant. Like other programming languages, a reference to an object is the only thing stored in the variable/constant. (Sidebar: Ruby does have the facility to “freeze” referenced objects from being modified, which as far as I know, isn’t an ability offered in many other languages.)

So here’s my question: when you re-assign a value into a constant, you get a warning like so:

>> FOO = 'bar'
=> "bar"
>> FOO = 'baz'
(irb):2: warning: already initialized constant FOO
=> "baz"

Is there a way to force Ruby to throw an exception instead of printing a warning?

Politeness

by Ben

Sometimes Rubyists can be a little rude. To help fix that problem, I did what I do best: I wrote a Ruby script.

From the GitHub repository:

Require your Ruby code to be more polite: require ‘politeness’

Inspired by INTERCAL, this library makes Ruby programmers say “please” more often. If Ruby doesn’t like how brash or cocky you’re being, it will stop in the middle of your code and tell you so:

"Not polite enough!"

However, you can say please too much. In that case, Ruby will throw its hands up and tell you that you’re:

"Too polite!"

Most libraries are very rude, so be sure to require politeness after including them (unless you intend to make them a little more well-mannered).

HTMLDiff in Ruby

by Ben

I ran into a problem using the HTMLDiff code found in i2 for doing nice diffs in Ruby. Of course, you’ll probably want to use in Rails, but if you are running Rails 2.1 (or maybe even other versions), you’ll want to use an updated version instead.

Update: It looks like this HTMLDiff code is no longer available at the provided links. I think I still have a copy somewhere, let me know if you need it by leaving a comment.