Class: Array

Inherits:
Object show all
Defined in:
lib/mini_sanity/array.rb

Instance Method Summary collapse

Instance Method Details

#assert_length!(length, name = nil) ⇒ self

Checks that the Array matches a given length, and returns the Array unmodified. If the Array fails this check, an exception is raised.

Examples:

coord = [0, 0, 0]
coord.assert_length(3)!  # == [0, 0, 0]

coord = [0, 0]
coord.assert_length(3)!  # raises exception

coord = [0, 0]
coord.assert_length(2..3)!  # == [0, 0]

Parameters:

  • length (Integer, Range<Integer>)

    length to match

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



24
25
26
27
28
29
30
31
# File 'lib/mini_sanity/array.rb', line 24

def assert_length!(length, name = nil)
  if !(length === self.length)
    raise MiniSanity::Error.new(name,
      "#{self.class} having #{length} elements",
      self.inspect)
  end
  self
end

#refute_length!(length, name = nil) ⇒ self

Checks that the Array does not match a given length, and returns the Array unmodified. If the Array fails this check, an exception is raised.

Examples:

some = ["one"]
some.refute_length!(0)  # == ["one"]

some = []
some.refute_length(0)!  # raises exception

many = ["one", "many"]
many.refute_length!(0..1)  # == ["one", "many"]

Parameters:

  • length (Integer, Range<Integer>)

    length to not match

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



54
55
56
57
58
59
60
61
# File 'lib/mini_sanity/array.rb', line 54

def refute_length!(length, name = nil)
  if length === self.length
    raise MiniSanity::Error.new(name,
      "#{self.class} not having #{length} elements",
      self.inspect)
  end
  self
end