Skip to content

Implies implementation

Jim Weirich edited this page May 24, 2013 · 2 revisions

Ruby doesn't supply an :implies method, but I find it useful for certain kinds of assertions. Here's a possible implementation:

class FalseClass
  def implies(consequent=false)
    true
  end
end

class NilClass
  def implies(consequent=false)
    true
  end
end

class Object
  def implies(consequent=false)
    if block_given?
      yield
    else
      consequent
    end
  end
end

Usage:

  (client.rich?).implies(client.money > LOTS)
  (! expression.reducible?).implies { expect { expression.reduce() }.to raise_error }