Sunday, November 8, 2015

Wow, really?

I don't have much time to write tonight since I've been making good on my vow to go through every problem in the 4 chapters covered by the Languages exam. One pearl that I missed in the lectures (or maybe it wasn't covered as it's a bit esoteric) is the parameter declarations for Ruby. I've never programmed in Ruby and I haven't done a whole lot in any other scripting language, so I'll admit I'm not an expert on the use case, but this seems needlessly whacky:

list = [2, 4, 6, 8]
def tester(p1, p2, p3, *p4)
...
end
tester('first', mon => 72, tue => 68, wed => 59, *list)
manages to produce a parameter list where
p1 is 'first'
p2 is {mon => 72, tue => 68, wed => 59}
p3 is 2
p4 is [4, 6, 8]
I have no problem with the literal or key/value pairs; it's splitting the list across p3 and p4 that makes my head spin. When would you intentionally do this? And, if you didn't mean to do it, why not let the compiler (well, interpreter in Ruby's case) catch it?

No comments:

Post a Comment