Created
April 11, 2011 20:01
-
-
Save blatyo/914206 to your computer and use it in GitHub Desktop.
Playing with ruby's ability to replace keywords.
This file contains hidden or 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
| 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