This year’s Ruby Hoedown is happening in Nashville again on September 3rd and 4th. I’m really looking forward to attending. The quality of the talks combined with the smaller attendance size makes for some great hallway conversations. Last year’s Hoedown was at the Opryland hotel which was a stellar venue. I have seriously never seen such a large hotel. Unfortunately, it can’t be used this year due to the recent flooding. But the new venue, the Hilton Downtown, looks really nice as well. As before, the Hoedown is completely free (as in beer) and talk proposals are currently being accepted. Are you going?
Category: Ruby
-
RubyConf in New Orleans
This year’s RubyConf is being held in New Orleans on November 11th – 13th.
Count me in.
I’ve only driven through the area once so it’ll be interesting to make a longer visit. Although I’m ultimately keeping my fingers crossed for a Raleigh RubyConf one of these days. Hey, I can dream.
-
test_spec_on_rails now runs on Rails 2.3
If anyone still happens to be using test_spec, you’ll be thrilled to know that the test_spec_on_rails plugin is now compatible with Rails 2.3. It has also been converted to a gem. Install with:
sudo gem install test_spec_on_rails
Add to your Rails app’s test.rb like so:
config.gem 'test_spec_on_rails'
Enjoy the goodness of test-spec helpers from inside your Rails tests. Fork and submit patches via the GitHub project. Tell your friends. Donate money. Vote for Pedro.
-
Processing malformed files with FasterCSV
On a recent project, I had to implement a CSV parser that would gracefully handle malformed files. I’m talking about files with unescaped quotes, wacky UTF-8 chars, and various other abominations of nature.
I originally assumed FasterCSV would handle this automagically, but it turns out that the library’s most commonly used methods are pretty strict when it comes to handling CSV files.
For example, parsing a malformed file one line at a time will result in an exception being thrown, even before any rows are yielded to the block:
FasterCSV.foreach("malformed.csv") do |row| # use row here... endNot cool! I managed to get around this by manually looping over each row and rescuing a malformed CSV exception if one gets thrown:
FasterCSV.open("malformed.csv", "rb") do |output| loop do begin break unless row = output.shift # use row here... rescue FasterCSV::MalformedCSVError => e # handle malformed row here... end end endAnyone have a better way to do this?
-
Time warping gem goodness
The time zone warp code I posted about last week is now a gem:
sudo gem install time-zone-warp
To configure in your Rails app, add this line to the bottom of test.rb:
config.gem 'time-zone-warp', :lib => 'time_zone_warp'
You can also fork the code from the project on GitHub.
-
Time zone warp
One of my Rails projects makes heavy use of time zones. I’ve run into some issues writing good tests for this type of thing. In particular, I’ve needed my tests to run within a time zone outside my own. But I don’t want to permanently change the time zone within the scope of the entire test run. I ended up coding this handler:
module ZoneWarp def pretend_zone_is(zone) original_zone = Time.zone begin Time.zone = zone yield ensure Time.zone = original_zone end end end Test::Unit::TestCase.send(:include, ZoneWarp)Simply stick this code in a file inside your
config/initializersdirectory (or include it from test_helper.rb or spec_helper.rb if you insist on doing it the right way) and you’re all set to write tests like this:test "code works in other time zones" do pretend_zone_is "Mountain Time (US & Canada)" do # assertions go here end end -
Learn about Prawn at raleigh.rb on August 18th
I’ll be giving a presentation about Prawn at this month’s raleigh.rb meetup. Prawn is a Ruby gem that enables fast PDF generation. It is a dramatic improvement over previous libraries like PDF::Writer. It can be used standalone or inside your Rails applications. The markup is powerful and relatively painless to use. I hope you can join us for the fun on August 18th at Red Hat HQ.
-
Audio interview for RubyRX 2009
Jared Richardson just posted a series of interviews in anticipation of the upcoming RubyRX/AgileRX conference taking place in Reston, Virginia in September. In my interview we discuss iPhone development, MacRuby, Git, and testing frameworks.I’m really looking forward to presenting again at RubyRX. I’ll be giving two talks this year. Git with Ruby will explore the Git source control system and how Ruby can take advantage of it. In Which Ruby Testing Framework Should I Use? we’ll briefly examine several leading testing frameworks and study the pros and cons of each. You’ll leave fully prepared to pick the best framework for your next project.
Let me know if you’re coming to the conference this year and we can link up in Reston. If you haven’t registered yet, what are you waiting for? RubyRX is a chance to network with the best and brightest developers in the area, and hear from thought leaders like Andy Hunt, Rich Kilmer, Joe O’Brien, and Chad Fowler. It’s a great way to keep your skills sharp in a down year.
-
Lindo testing helper gets some love
Lindo helps you write and verify Rails functional and integration tests by opening the HTTP response body in the default browser for inspection. This can be a real time-saver when you’re trying to figure out why your
assert_selectorhave_tagcalls aren’t passing.In its initial version, Lindo assumed that your app was running at localhost:3000 (a fair assumption given the prevalence of Mongrel last year). Now that Passenger is on the scene, something better needed to be done. The reliance on a running app server was a disadvantage to begin with. Now Lindo doesn’t require anything to be running. It dumps the HTML to disk, fixes any relative asset URLs, and opens the file using your default browser.
Once you’ve written your first test with the assistance of Lindo, you won’t want to go back!
Lindo was developed by my company, Adeptware, and can be pulled from GitHub. I’ve also posted a brief introduction to Lindo and some basic installation instructions.
-
How to safely transpose Ruby arrays
Ruby arrays have this handy method called
transposewhich takes an existing 2-dimensional array (i.e. a matrix) and flips it on its side:>> a = [[1,2], [3,4], [5,6]] >> puts a.transpose.inspect [[1, 3, 5], [2, 4, 6]]
Each row becomes a column, essentially. This is fine and dandy for polite arrays. If one of the rows in the original array is not as long as the others, though, Ruby chunders thusly:
>> a = [[1,2], [3,4], [5]] >> a.transpose IndexError: element size differs (1 should be 2) from (irb):3:in `transpose' from (irb):3
That ain’t pretty, especially if the intent behind using
transpose is to render data in a nice columnar fashion. For example, what if we wanted to render a list of high school courses in columns, one column per semester? Grouping the courses by semester and then transposing would do the trick, but only if there were exactly the same number of courses taken each semester. If even one semester differs, Ruby will blow up. What we really want is for Ruby to just ignore the fact that each grouping may have differently sized arrays and transpose anyway, filling in the empty spaces with nils.Here’s how to do just that:
class Array def safe_transpose result = [] max_size = self.max { |a,b| a.size <=> b.size }.size max_size.times do |i| result[i] = Array.new(self.first.size) self.each_with_index { |r,j| result[i][j] = r[i] } end result end endNow we call
safe_transposeon our matrix of courses and Ruby does the right thing. It calculates the length of the longest row and uses that as the baseline to perform the transposition. So our original example becomes:>> a = [[1,2], [3,4], [5]] >> puts a.transpose.inspect [[1, 3, 5], [2, 4, nil]]
Nice and neat. Caveats: the code above hasn’t been refactored or tested. Your mileage may vary. If you see a better way to do this, let me know and I’ll post an update.
