Skip to content

Instantly share code, notes, and snippets.

@naohaq
Created July 19, 2012 05:03
Show Gist options
  • Save naohaq/3140863 to your computer and use it in GitHub Desktop.
Save naohaq/3140863 to your computer and use it in GitHub Desktop.
Represent then/else clauses as blocks.
#!/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