Red Squirrel Reflections
Dave Hoover explores the psychology of software development
Fri, 13 May 2005Domain-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) endBletch. 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.