Class: NeoScout::ConstrainedSet

Inherits:
Set
  • Object
show all
Defined in:
lib/neoscout/tools.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &elem_test) ⇒ ConstrainedSet

Returns a new instance of ConstrainedSet.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/neoscout/tools.rb', line 51

def initialize(*args, &elem_test)
  @elem_test = elem_test
  case
    when args.length == 0
      super
    when args.length == 1
      args = args[0]
      raise ArgumentError unless (args.all? &@elem_test)
      super args
    else
      raise ArgumentError
  end
end

Instance Method Details

#<<(elem) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
# File 'lib/neoscout/tools.rb', line 69

def <<(elem)
  raise ArgumentError unless valid_elem?(elem)
  super elem
end

#valid_elem?(elem) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/neoscout/tools.rb', line 65

def valid_elem?(elem)
  @elem_test.call(elem)
end