Class | ArrayOf |
In: |
lib/rbot/irc.rb
|
Parent: | Array |
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.
element_class | [R] |
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 374 374: def initialize(kl, ar=[]) 375: raise TypeError, "#{kl.inspect} must be a class name" unless kl.kind_of?(Class) 376: super() 377: @element_class = kl 378: case ar 379: when Array 380: insert(0, *ar) 381: else 382: raise TypeError, "#{self.class} can only be initialized from an Array" 383: end 384: end
Overloaded from Array#concat, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 460 460: def concat(ar) 461: super(ar) if internal_will_accept?(true, *ar) 462: end
Overloaded from Array#insert, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 467 467: def insert(idx, *ar) 468: super(idx, *ar) if internal_will_accept?(true, *ar) 469: end
# File lib/rbot/irc.rb, line 386 386: def inspect 387: self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>" 388: end
Overloaded from Array#push, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 481 481: def push(*ar) 482: super(*ar) if internal_will_accept?(true, *ar) 483: end
Overloaded from Array#replace, checks for appropriate class of argument elements
# File lib/rbot/irc.rb, line 474 474: def replace(ar) 475: super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar) 476: end
Overloaded from Array#unshift, checks for appropriate class of argument(s)
# File lib/rbot/irc.rb, line 487 487: def unshift(*els) 488: els.each { |el| 489: super(el) if internal_will_accept?(true, *els) 490: } 491: end
This method checks that all elements are of the appropriate class
# File lib/rbot/irc.rb, line 414 414: def valid? 415: will_accept?(*self) 416: 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 421 421: def validate 422: raise TypeError unless valid? 423: end
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 395 395: def internal_will_accept?(raising, *els) 396: els.each { |el| 397: unless el.kind_of?(@element_class) 398: raise TypeError, "#{el.inspect} is not of class #{@element_class}" if raising 399: return false 400: end 401: } 402: return true 403: end