Skip to content

Instantly share code, notes, and snippets.

@blatyo
Created April 11, 2011 20:01
Show Gist options
  • Select an option

  • Save blatyo/914206 to your computer and use it in GitHub Desktop.

Select an option

Save blatyo/914206 to your computer and use it in GitHub Desktop.
Playing with ruby's ability to replace keywords.
class Ca
def initialize(obj)
@obj = obj
@steps = []
end
def wen(*conditions)
@steps << Proc.new if conditions.any?{|condition| condition === @obj}
end
def default
@steps << Proc.new
end
def result
@steps.first.call
end
class << self
def se(obj, &block)
kase = self.new(obj)
kase.instance_eval(&block)
kase.result
end
end
end
#Example usage
["", :a, 1000000000000000000000, 1, {}, []].each do |obj|
Ca.se obj do
wen String, Symbol do
puts "stringish"
end
wen Bignum, Integer do
puts "numericalish"
end
default do
puts "otherish"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment