class ArrayOf
ArrayOf
is a subclass of Array
whose elements are supposed to be all of the same class. This is not intended to be used directly, but rather to be subclassed as needed (see for example Irc::UserList
and Irc::NetmaskList
)
Presently, only very few selected methods from Array
are overloaded to check if the new elements are the correct class. An orthodox? method is provided to check the entire ArrayOf
against the appropriate class.
Attributes
Public Class Methods
Create a new ArrayOf
whose elements are supposed to be all of type kl, optionally filling it with the elements from the Array
argument.
# File lib/rbot/irc.rb, line 375 def initialize(kl, ar=[]) raise TypeError, "#{kl.inspect} must be a class name" unless kl.kind_of?(Class) super() @element_class = kl case ar when Array insert(0, *ar) else raise TypeError, "#{self.class} can only be initialized from an Array" end end
Public Instance Methods
Overloaded from Array#&, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 434 def &(ar) r = super(ar) ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r) end
Overloaded from Array#+, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 441 def +(ar) ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar) end
Overloaded from Array#-, so that an ArrayOf
is returned. There is no need to check the validity of the elements in the argument
# File lib/rbot/irc.rb, line 448 def -(ar) ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar) end
Overloaded from Array#<<, checks for appropriate class of argument
# File lib/rbot/irc.rb, line 428 def <<(el) super(el) if internal_will_accept?(true, el) end
Overloaded from Array#concat, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 461 def concat(ar) super(ar) if internal_will_accept?(true, *ar) end
We introduce the ‘downcase’ method, which maps downcase() to all the Array
elements, properly failing when the elements don’t have a downcase method
# File lib/rbot/irc.rb, line 497 def downcase self.map { |el| el.downcase } end
Overloaded from Array#insert, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 468 def insert(idx, *ar) super(idx, *ar) if internal_will_accept?(true, *ar) end
# File lib/rbot/irc.rb, line 387 def inspect self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>" end
Overloaded from Array#push, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 482 def push(*ar) super(*ar) if internal_will_accept?(true, *ar) end
Overloaded from Array#replace, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 475 def replace(ar) super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar) end
Overloaded from Array#unshift, checks for appropriate class of argument(s)
# File lib/rbot/irc.rb, line 488 def unshift(*els) els.each { |el| super(el) if internal_will_accept?(true, *els) } end
This method checks that all elements are of the appropriate class
# File lib/rbot/irc.rb, line 415 def valid? will_accept?(*self) end
This method is similar to the above, except that it raises an exception if the receiver is not valid
# File lib/rbot/irc.rb, line 422 def validate raise TypeError unless valid? end
This method checks if the passed arguments are acceptable for our ArrayOf
# File lib/rbot/irc.rb, line 409 def will_accept?(*els) internal_will_accept?(false, *els) end
Overloaded from Array#|, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 454 def |(ar) ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar) end
Private Instance Methods
Private method to check the validity of the elements passed to it and optionally raise an error
TODO should it accept nils as valid?
# File lib/rbot/irc.rb, line 396 def internal_will_accept?(raising, *els) els.each { |el| unless el.kind_of?(@element_class) raise TypeError, "#{el.inspect} is not of class #{@element_class}" if raising return false end } return true end