On Sun, 9 Jan 2005, Mathieu Bouchard wrote:
And so is [rubysprintf]. well, the object (class) itself is not wonderful by itself, you have to attribute that to Ruby itself. Here's the source code for [rubysprintf]:
This was a nice blunder of mine, which could have been 95-99% unnoticed if I wasn't bringing attention to it right now. I almost did not notice it.
Ok, here's a really nice (NOT!) methodology for debugging code:
0. write some part of the source code, in a few minutes, while doing something else, and never look back, because it does just what you happen to ask of it.
1. post some small example of your source code on a much-read int'l mailing-list, and bring particular attention to that snippet.
2. read the mail when it comes back through the mailing-list software and stare at it stupidly.
3. find a really stupid bug in the really small example.
So, for the few who can code in Ruby (or, particularly, are currently learning to) you can, from here, just skip back to the previous mail and try to find the bug, as a quiz.
. . .
Answer: Assigning to the block-variable x has no effect, as it is just a copy of a slot of the array, instead of being the slot itself (contrast to Perl's almost-equivalent "foreach my $x (@$a) { ... }").
So, to convert all symbols of a list to strings, and not touch the floats, one could change that line like this:
a.each {|x| x=x.to_s if Symbol===x }
a.map! {|x| case x when Symbol; x.to_s ## cast to string... else x ## else keep it as is end } ## map! is like each but replaces each element by the return value
or (closer in style to the former):
a.map! {|x| if Symbol===x then x.to_s else x end }
but then, my actual replacement looks like this:
... That's it, I just deleted the line. The reason is that, in the handling of "%s", Ruby already applies .to_s on whichever it gets, so both Symbols and Floats are accepted in %s.
However, in several other cases, the .to_s is not automatic (and some of my externals have lines of code like the above)
I thought about mapping Pd symbols directly to Ruby strings instead, which would be slightly less trouble, but it's sort of too late now... Anyway I reserve Ruby's string type to match to the upcoming Pd strings (whatever they will be).
_____________________________________________________________________ Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju