Red Squirrel Reflections
Dave Hoover explores the psychology of software development

Dave Hoover
dave.hoover@gmail.com

Categories
All [Atom]
Craftsmanship [Atom]
Dynamic [Atom]
Intersection [Atom]
Learning [Atom]
Links [Atom]
Polyglot [Atom]
Projects [Atom]
XP [Atom]
Old Blog

Obtivian Blogs

Andy Maleh
Colin Harris
Fred Polgardy
Jim Breen
Kevin Taylor
Todd Webb
Turner King
Tyler Jennings

Archives

March 2009 (1)
January 2009 (1)
December 2008 (1)
October 2008 (3)
September 2008 (1)
June 2008 (4)
April 2008 (3)
March 2008 (1)
February 2008 (1)
August 2007 (1)
July 2007 (1)
June 2007 (1)
May 2007 (4)
April 2007 (3)
March 2007 (5)
February 2007 (6)
January 2007 (6)
December 2006 (10)
November 2006 (5)
October 2006 (8)
September 2006 (8)
August 2006 (5)
July 2006 (12)
June 2006 (7)
May 2006 (5)
April 2006 (5)
March 2006 (4)
February 2006 (2)
January 2006 (5)
December 2005 (5)
November 2005 (3)
October 2005 (3)
September 2005 (6)
August 2005 (4)
July 2005 (7)
June 2005 (14)
May 2005 (6)
April 2005 (8)
March 2005 (9)
February 2005 (11)
January 2005 (16)
Old Archives

 

Fri, 13 May 2005

The Ruby White Belt

One of the attributes of some of my favorite patterns books are visual representations of the relationships between the patterns. Particularly Domain-Driven Design and Organizational Patterns of Agile Software Development. Therefore, I want to be able to easily generate a visual representation of the apprenticeship pattern language.

Taking a cue from Jim Coplien's OrganizationBookPatternTable and corresponding OrgPatternsMap, I started a page on my private wiki that mapped some of the dependencies for Accurate Self-Assessment. I then began developing a Ruby script to grab my wiki page, parse it, and convert the patterns into dot format, which I would then pipe into dot to create something like this

And it was all going very smoothly until I needed to use a regular expression to grab all the instances of a specific regexen from a single line. In Perl this is accomplished like this:

	$line = 'Mac Mini, 1.25GHz $499.00 1.42GHz $599.00';
	@prices = $line =~ /\$(\d+)/g;
The @prices array contains 499 and 599. Perl and Ruby are so similar, I assumed it would be easy to do the same in Ruby. I tried /\$(\d+)/g and Ruby told me g was an uknown regexp option. I scoured the Regexp and MatchData docs but came up with nothing. I pinged a couple Rubyists on IM and I was told that to do the same thing in Ruby, I would need to use String.gsub, which is odd, since you're not actually substituting anything. Here's the eqivalent Ruby code.
	line = "Mac Mini, 1.25GHz $499.00 1.42GHz $599.00"
	prices = []
	line.gsub( /\$(\d+)/ ) do
		prices.push($1)
	end
Bletch. I was disappointed. How could a language as beloved as Ruby fall down on such a fundamental text-processing task?

And then I pondered an apprenticeship pattern that Ade drafted yesterday: Wear the White Belt. A key thought from the pattern:

"We have to be able to put aside our past experiences and preconceptions to allow in new knowledge."
Determined to Wear the White Belt, I went back to the documents once more, this time not trying to translate my Perl approach into Ruby, but to learn the Ruby way. And surprise, surprise, I found String.scan. Now this is what I've come to expect from Ruby...
	line = "Mac Mini, 1.25GHz $499.00 1.42GHz $599.00"
	prices = line.scan( /\$(\d+)/ )

Ahhhh, the power of Perl combined with the elegance of Japanese design. Hold onto that white belt.

[/craftsmanship] permanent link


powered by blosxom