Skip to content

Instantly share code, notes, and snippets.

@alexch
Created May 15, 2009 15:47
Show Gist options
  • Save alexch/112262 to your computer and use it in GitHub Desktop.
Save alexch/112262 to your computer and use it in GitHub Desktop.
# 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