Created
August 3, 2012 09:20
-
-
Save hugecannon/3246228 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (a OR b OR c) | |
if a | |
end if | |
if b | |
end if | |
if c | |
end if | |
[code] | |
[code] | |
[code] | |
end if |
My take in Ruby:
class Switch
def initialize item, &block
@item = item
@match = false
instance_eval &block
if @match && !@match_block.nil?
instance_eval &@match_block
end
end
def eq comparator, &block
if @item == comparator
instance_eval &block
@match = true
end
end
def match &block
@match_block = block
end
end
def switch item, &block
Switch.new item, &block
end
test = "a"
switch test do
eq "a" do
puts "A"
end
eq "b" do
puts "B"
end
match do
puts "run if either matches"
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB that's in coffeescript, using underscore.js.