Created
May 15, 2009 15:47
-
-
Save alexch/112262 to your computer and use it in GitHub Desktop.
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
# Ruby implementation of Smalltalk's "if" construction. See also | |
# http://pozorvlak.livejournal.com/94558.html | |
# | |
# Note that since I can't cleanly pass in more than one anonymous block to a | |
# method in Ruby, I have to implement it as two methods rather than | |
# Smalltalk's cleaner "ifTrue:ifElse:" single method. This means that in order | |
# to get the syntax for chaining, I lose Ruby's ability to return a value from | |
# "if", which is pretty cool in itself. | |
# | |
class TrueClass | |
def ifTrue | |
yield | |
self | |
end | |
def ifFalse | |
self | |
end | |
end | |
class FalseClass | |
def ifTrue | |
self | |
end | |
def ifFalse | |
yield | |
self | |
end | |
end | |
[1,2,3,4].each do |x| | |
(x % 2 == 0). | |
ifTrue {puts "#{x} is even"}. | |
ifFalse {puts "#{x} is odd"} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment