From: "Guy N. Hurst" Date: 2002-04-21T07:58:57+09:00 Subject: Re: RCR: key value mapping for sprintf/% Carlo Teubner wrote: > ... > Dissecting your code, I found some very minor things I would have done slightly differently. Annotations below. > > class String > alias old_sf % > def %(vars) # [1] > re = /%\((\w*?)\)([-#0\s+']*\d*(?:\.\d*)?[bcdeEfgGiosxXu])/ # [2] > case vars > when Hash > gsub(re) { |m| > if vars[$1].nil? # [3], [4] > m # [5] > else > "%#$2" % vars[$1] # [4] > end > } > else > old_sf vars > end > end > end > > Annotations: > > [1] Why the default value, {}? Normally, String#% complains if you give it too many or too little arguments (or none at all). I think this behaviour should be kept. > I don't like complaints ;-) However, I will incorporate this change and see how it goes. > [2] I changed (\([\w]*?\)) to \((\w*?)\). That way, the parentheses won't become part of $1. > Ok, sounds fair enough, since I never ended up using them like I first thought I would, and if in the future I need to, I can always put them in explicitly. > [3] I removed the begin..rescue construct, because I think that an ArgumentError thrown by old_sf should just be passed on to the caller. Or did you think of other kinds of exceptions? > I liked it absorbing all errors. However, I will take it out. The coder can put his own exception block in, I guess. > [4] Thanks to [2], we don't need to remove the parentheses from $1 and can directly use $1. > Right. > [5] if vars[$1].nil?, then vars.default will also be nil. > True. But you have actually uncovered a tiny bug here. I have modified it to use vars.key?$1 instead. > Sorry for the nitpicking... probably I am even wrong in some places. But I found your code so intriguing that I had to play around with it a bit :) > > -Carlo Thanks for the input! I am also incorporating some input from Paul Brannon. Guy N. Hurst