Wonder of the When-Be-Splat #
Well, okay, yes, we already know Ruby is expressive. And, if I know you, you’ll see a drop of awesomeness in:
BOARD_MEMBERS = ['Jan', 'Julie', 'Archie', 'Stewick'] HISTORIANS = ['Braith', 'Dewey', 'Eduardo'] case name when *BOARD_MEMBERS "You're on the board! A congratulations is in order." when *HISTORIANS "You are busy chronicling every deft play." end
I was refactoring a parser and it occured to me that I could just do a when *tokens.keys at one point.  Ruby treats it just like a list of conditions.  Consider Olympic ice dancing out-graced!
 Addendum: I’m just gonna log a few of the other discoveries implied here, just to prove how handily this waxes if..include?’s sorry snout.
 case name when "Arthur", *BOARD_MEMBERS
   "Either you are a board member... or you are Arthur." 
 end
 case name when *BOARD_MEMBERS|HISTORIANS
   "We welcome you all to the First International
    Symposium of Board Members and Historians Alike." 
 end


 

yerejm
Say goodbye to
include?,case. She wasn’t expressive enough.Matt Todd
That’s beautiful! It communicated to me on three different levels! :-)
M.T.
humdrum
That is nicely expressive.. but why does it work?
Justin Palmer
Now thats aesthetics even a designer can love. :-)
andrew
Ruby’s case has a lot of goodness, but I never knew.
Danno
Beauti-tastic.
ConFused
That’s cool, but, as a newbie, I don’t see what it going on under the covers. Can someone explain it? Is this some kind of regexp matching?
Thanks!
why
humdrum: In times past, the splat has been used to pitch an array into a method as the method arguments:
args = ["/tmp/a-gust-of-wind.txt", "w+"] open(*args) { |f| f << "..wchhoooo..." }More recently, the splat is now widely used in place of
to_a.So, juxtapose this with Ruby’s
whenstatement which can take multiple conditions and you’ve got it.Joey__
Very nice, you learn something new every day!
By the way, you left off a ’ in front of Julie.
Thanks for the tip!
Josh
cilibrar
That was really awesome why. You never cease to amaze me!!!
why
Check. Sorry comments were defunct.
slumos
This should be a haiku, but I didn’t have time. With all the deep hacking and evil going on lately, I wonder if we’ve been distracted from the simple beauty of Ruby.
Cherry blossoms.
weeksie
It feels all pattern-match-y :)
Branstrom
“case name when” doesn’t feel all fuzzy inside, shouldn’t “when” be “is” for the first line? :p maybe im getting spoiled with all the english-simulating syntax otherwise around :p :p
tilman
This only works with Ruby 1.9, right?
WWWWolf
A programming language with a sane syntax? Will the the wonders this world has ever end!
Joey__
tilman:No, it works on Ruby 1.8.4, not sure about Ruby 1.8.2 though.
a=[1,2,3,4,4]a.zip([a]) => [[1, [1, 2, 3, 4, 4]], [2, nil], [3, nil], [4, nil], [4, nil]]a.zip([*a]) => [[1, 1], [2, 2], [3, 3], [4, 4], [4, 4]]a.zip(*[a]) => [[1, 1], [2, 2], [3, 3], [4, 4], [4, 4]]a.zip(a) => [[1, 1], [2, 2], [3, 3], [4, 4], [4, 4]]sporkmonger
tilman: It works fine in 1.8.2 as well.
kode
I need to remember this. Might come in useful some time.
tilman
Mmh, the first example in the article doesn’t work for me on Ruby 1.8.4 :/
joey__
name has to be defined, you cannot copy and paste this code, it won’t work.
tilman
I’m not that stupid :P
Chuck
Works for me. What error are you getting?
joey__
tilman Its best to think of the most obvious things first :)
tilman
I just realized the result of the case expression was never written to stdout or something :o It works nicely if I add “puts” somewhere ;) tilman—
jvoorhis
Destructuring is delicious
Comments are closed for this entry.