Method: Object#refute!

Defined in:
lib/mini_sanity/assert.rb

#refute!(pattern = MiniSanity::TRUTHY, hint: nil) ⇒ self #refute!(pattern = MiniSanity::TRUTHY, hint: nil) {|itself| ... } ⇒ self

Checks that a given pattern does not match the Object (or a derivative value), and returns the Object. Raises an exception if the pattern matches.

If a block is given, the Object is yielded to the block, and the derivative value returned by the block is checked instead.

Examples:

Refute truthy

false.refute!  # == false
"bad".refute!  # raises exception

Refute forbidden value

"foo".refute!("bad")  # == "foo"
"bad".refute!("bad")  # raises exception

Refute Set of prohibited values

"foo".refute!(Set["bad", "worse"])  # == "foo"
"bad".refute!(Set["bad", "worse"])  # raises exception

Refute Class

25.refute!(Float)   # == 25
2.5.refute!(Float)  # raises exception

Refute Regexp

"foo".refute!(/^ba/)  # == "foo"
"bad".refute!(/^ba/)  # raises exception

Refute Range

2.refute!(5..)  # == 2
5.refute!(5..)  # raises exception

Refute truthy derivative value

[2].refute!(&:empty?)  # == [2]
[].refute!(&:empty?)   # raises exception

Refute derivative value

[2].refute!(1.., &:length)     # == [2]
[2, 5].refute!(1.., &:length)  # raises exception

Overloads:

  • #refute!(pattern = MiniSanity::TRUTHY, hint: nil) ⇒ self

    Parameters:

    • pattern (#===) (defaults to: MiniSanity::TRUTHY)
    • hint (String) (defaults to: nil)

      Hint to include in the error message

    Returns:

    • (self)

    Raises:

  • #refute!(pattern = MiniSanity::TRUTHY, hint: nil) {|itself| ... } ⇒ self

    Parameters:

    • pattern (#===) (defaults to: MiniSanity::TRUTHY)
    • hint (String) (defaults to: nil)

      Hint to include in the error message

    Yield Parameters:

    • itself (self)

    Yield Returns:

    • (Object)

      Derivative value

    Returns:

    • (self)

    Raises:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mini_sanity/assert.rb', line 147

def refute!(pattern = MiniSanity::TRUTHY, hint: nil, &block)
  result = block ? block.call(self) : self

  if pattern === result
    raise MiniSanity::Error.new("Refute failed", {
      "Value" => self.inspect,
      "Derived value (from #{MiniSanity::Error.describe_block(&block) || "block"})" =>
        (result.inspect if block),
      "Refute matches" => pattern.inspect,
      "Hint" => hint,
    })
  end

  self
end