Skip to content

Instantly share code, notes, and snippets.

@hugecannon
Created August 3, 2012 09:20
Show Gist options
  • Save hugecannon/3246228 to your computer and use it in GitHub Desktop.
Save hugecannon/3246228 to your computer and use it in GitHub Desktop.
if (a OR b OR c)
if a
end if
if b
end if
if c
end if
[code]
[code]
[code]
end if
@jbrunton
Copy link

jbrunton commented Aug 3, 2012

NB that's in coffeescript, using underscore.js.

@zofrex
Copy link

zofrex commented Aug 3, 2012

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