Archive for September, 2008

Hartford Ruby Brigade meeting tonight

If you’re in the area, feel free to drop in:

The Hartford Ruby Brigade’s September meeting is tonight, from 6 to 8 PM, at GeeZeo’s offices on 750 Main St, Suite 1314, in Hartford (next to the CVS).

The big news is Addison Wesley has a ticket for the Voices That Matter: Professional Ruby Conference this November, and we’ll raffle it off. It looks like a sweet conference, go take a look. You have to be there to win, so if you’ve been waiting for the right time to make an appearance, this is it. And since only one of us can win the ticket, we’ll also raffle off a great Ruby book, Hal Fulton’s The Ruby Way. A big thanks to Addison Wesley!

Aaron will give his long-promised IronRuby talk, and I’ll show a mini-project of mine that generates MSWord .docs from Textile, with RedCloth. It’s flashy, I promise.

Remember, if you have a topic you’d like to talk about, or one you’d like to hear about, or even if you just have an idea for what to do for the evening, don’t be shy, speak up! The group is what we make it, and it belongs to all of us.

As always, we’ll have free pizza and soda, courtesy of Sun Microsystems.

Hope to see you there!

Use Ruby’s method_missing to see what happens to an object

Lately I’ve been generating MS Word .docs from text files, with RedCloth and Hpricot (thank you, why, for both of those), and win32ole. It’s been fun, except for the OLE parts: the ruby bridge is great, but the OLE API itself is strange, good documentation is sparse, and things get…flaky, sometimes.

In troubleshooting a bug that turns bold text on and off at the wrong times, I thought “it’d be nice if I could see all the calls that happen to this ’selection’ object, in order they happen. Maybe I don’t realize I’m turning the ‘bold’ on at the wrong times.” Enter the Echoer:

class Echoer
    def initialize(name)
        @name = name
    end
    def method_missing(method_name, *args)
        puts "#{@name}.#{method_name}: #{args * ', '}"
        Echoer.new("#{@name}.#{method_name}")
    end
end

The Echoer is a stand-in for a regular object. Whenever a method is called on it, it prints its own name, the name of the method called, and the arguments.

def get_me_my_object
    # RealObject.new
    Echoer.new('obj')
end
obj = get_me_my_object
obj.puts 'Hello there'
obj.name = "Puddin'head Wilson"

# prints:
obj.puts: Hello there
obj.name=: Puddin'head Wilson

Each method call returns a new Echoer, whose name is based on its name, and the method name, so you can chain method calls.

obj.next.upcase.match /pattern/

# prints:
obj.next:
obj.next.upcase:
obj.next.upcase.match: (?-mix:pattern)

I should probably make Echoer a BlankSlate object, but I haven’t run into a need for it just yet. I could also inspect the method’s arguments with args.map { |arg| arg.inspect }, so you can tell strings and symbols apart.

Back in OLE-land, I replaced all instances of the selection object with Echoer.new('selection'), re-ran my code, and watched the output. I still haven’t found the source of the bug, but at least I know I’m not turning bold on or off when I don’t expect to.

Thanks to Michael Feathers for this idea. He wrote on the Beautiful Code blog about Spelunking Ruby Methods with Pebbles, but it seems O’Reilly took the blog down. All that’s left are the links from sites like reddit…does anyone know if the content is floating around somewhere out there?


Say Hello

danbernier [at] gmail [dot] com
Twitter @danbernier
Hartford.rb
LinkedIn

Tweeting:

  • Trying f.lux: http://stereopsis.com/flux/ Just watched a rather noticeable change as sunset hit. Weird, but cool. 22 hours ago
  • My first day limiting gmail, google rss, gwave, twitter, google calendar, & outlook to only 3 times a day. Went well, I like it! 22 hours ago
  • When MS sunsets XP, companies will get Win7, with IE8. Will that destroy IE6's market share? Will that solve our problems? 4 days ago
  • @bdowden Well, no, but clearing off the muck at least lets you REALIZE it's a car. Which is worth knowing. 5 days ago
  • Taming dependencies helps the real structure emerge. It's like cleaning swamp slime off a submerged car frame. 5 days ago

I’m a BackPack fan

Backpack

Categories