Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmshvetsov/cbcc6dd016881188be9e2f988dc4c41e to your computer and use it in GitHub Desktop.
Save dmshvetsov/cbcc6dd016881188be9e2f988dc4c41e to your computer and use it in GitHub Desktop.
How to avoid if/else branch condition in Ruby. By Yehuda Katz https://yehudakatz.com/2009/10/04/emulating-smalltalks-conditionals-in-ruby/. Please don't do it in production code or your gems.
class Object
def if_true
yield
self
end
def if_false
self
end
end
class NilClass
def if_true
self
end
def if_false
yeild
self
end
end
class FalseClass
def if_true
self
end
def if_false
yeild
self
end
end
# Then it make possible
something.if_true {
# The "true" case
}.if_false {
# The "false" case
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment