PDA

View Full Version : [RedHanded] Wonder of the When-Be-Splat


ruby-lang
March 9th, 2006, 11:45 AM
<p>Well, okay, yes, we already know Ruby is expressive. And, if I know you, you’ll see a drop of awesomeness in:</p>


<pre>
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
</pre>

<p>I was refactoring a parser and it occured to me that I could just do a <code>when *tokens.keys</code> at one point. Ruby treats it just like a list of conditions. Consider Olympic ice dancing out-graced!</p>


<p class="update"> <strong>Addendum:</strong> I’m just gonna log a few of the other discoveries implied here, just to prove how handily this waxes <code>if..include?</code>’s sorry snout.</p>


<pre>
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
</pre>

<a href="http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html" target="_blank">http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html</a>