codahale.com٭blog

Coda Hale lives in Berkeley, CA, where he writes about Ruby on Rails, usability, web design and development, and the occasional bit about bicycles.

A Ruby HOWTO: Writing A Method That Uses Code Blocks

One of the first things about Ruby which absolutely delighted me was its implementation of code blocks. They take a potentially ugly construct–anonymous delegates–and turn them into a readable, time-saving structure:


chunky_bacon = %w(moo hoo ha ha)
chunky_bacon.each { |cb|
	puts "--> #{cb}"
}

So how can we use this wonderful little bit of functionality in our own code? Well, break out a text editor and a terminal window, because it’s time to write some Ruby! Until the Tryptophan kicks in and we lurch around, belly exposed and bulging, wondering how long it’ll take until we’ve digested enough food to go back for another slice of pie. Happy Thanksgiving!

(continue reading…)

6 comments »