Created
July 19, 2012 05:03
-
-
Save naohaq/3140863 to your computer and use it in GitHub Desktop.
Represent then/else clauses as blocks.
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
#!/usr/bin/ruby1.9.1 -Ku | |
# -*- mode: ruby; coding: utf-8-unix -*- | |
class Conditional | |
class Else | |
def initialize(p,v) | |
@p = p | |
@v = v | |
end | |
def else | |
unless @p then | |
yield | |
else | |
@v | |
end | |
end | |
end | |
class Then | |
def initialize(p) | |
@p = p | |
end | |
def then | |
v = nil | |
if @p then | |
v = yield | |
end | |
return Else.new(@p,v) | |
end | |
end | |
def if() | |
p = yield | |
return Then.new(p) | |
end | |
end | |
c = Conditional.new | |
a = 3 | |
b = 1 | |
c.if{ a < b }.then{ puts "True!" }.else{ puts "False!" } | |
c.if{ a > b }.then{ puts "True!" }.else{ puts "False!" } | |
# Local Variables: | |
# ruby-indent-level: 3 | |
# indent-tabs-mode: nil | |
# End: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment