Regarding the readability point and string interpolation, we usually argue these two points: 1) presentation changes faster than anything else, and 2) separate that which changes from that which doesn’t.
]]>Are method invocations any cheaper than branches? Methods abstract an operation, but here the operation is the same in all cases: append onto a list. We only want to abstract the list. We’d be better off with no methods and a hash, I’d wager.
I’ve incorporated that change, Jonathan’s suggestion, and I’ve factored out a distribute method (whose counter warrants some local-static state; I used an instance variable). I now find the code much harder to understand.
]]>I have to agree with Jonathan. And for me, the readability comes from not having to look back and forth to see what variable is being substituted for some %x. Having the actual variable in its intended place helps me a lot, even if there is some extra junk in the way.
The only thing I might have done differently, and this is extremely nitpicky and just I method I have found enjoyable, is to split the adding of students into lists into separate methods. So methods called a, b, x, and something for absent. This way you can replace the entire if else ladder with just:
self.send(line[1])
Ruby is fairly inefficient with branching, so with larger applications and more taxing branches, ‘send’ can sometimes mean less work and less code. But for something like this it is purely preference.
]]>Ack. I’m getting old. I don’t think #{var} looks readable at all. You young punks…
]]>I love printf, too; but I’ve never seen anyone use it in Ruby except when actual formatting is needed. String interpolation preserves code readability, which is a big part of the Ruby way.
My other point was to highlight the convenient pairing of foo and foo! methods…not shuffle specifically.
I’ve always been a sucker for the % substitution. On a micro-scale, it separates the model from the presentation. Maybe.
Java’s got Collections.shuffle (don’t tell my CS 145 classes), C++’s got random_shuffle, Python’s got random.shuffle, R’s got sample, …
]]>%. Especially since there's no real formatting going on.
groups[i] = "#{groups[i]}, #{xs.pop}"
Ah, isn't it nice to have methods like shuffle and shuffle! built in?