Class: ArrayOf
Overview
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#element_class ⇒ Object
readonly
Returns the value of attribute element_class.
Instance Method Summary collapse
-
#&(ar) ⇒ Object
Overloaded from Array#&, checks for appropriate class of argument elements.
-
#+(ar) ⇒ Object
Overloaded from Array#+, checks for appropriate class of argument elements.
-
#-(ar) ⇒ Object
Overloaded from Array#-, so that an ArrayOf is returned.
-
#<<(el) ⇒ Object
Overloaded from Array#<<, checks for appropriate class of argument.
-
#concat(ar) ⇒ Object
Overloaded from Array#concat, checks for appropriate class of argument elements.
-
#downcase ⇒ Object
We introduce the ‘downcase’ method, which maps downcase() to all the Array elements, properly failing when the elements don’t have a downcase method.
-
#initialize(kl, ar = []) ⇒ ArrayOf
constructor
Create a new ArrayOf whose elements are supposed to be all of type kl, optionally filling it with the elements from the Array argument.
-
#insert(idx, *ar) ⇒ Object
Overloaded from Array#insert, checks for appropriate class of argument elements.
- #inspect ⇒ Object
-
#push(*ar) ⇒ Object
Overloaded from Array#push, checks for appropriate class of argument elements.
-
#replace(ar) ⇒ Object
Overloaded from Array#replace, checks for appropriate class of argument elements.
-
#unshift(*els) ⇒ Object
Overloaded from Array#unshift, checks for appropriate class of argument(s).
-
#valid? ⇒ Boolean
This method checks that all elements are of the appropriate class.
-
#validate ⇒ Object
This method is similar to the above, except that it raises an exception if the receiver is not valid.
-
#will_accept?(*els) ⇒ Boolean
This method checks if the passed arguments are acceptable for our ArrayOf.
-
#|(ar) ⇒ Object
Overloaded from Array#|, checks for appropriate class of argument elements.
Methods inherited from Array
#delete_one, #pick_one, #shuffle, #shuffle!
Constructor Details
#initialize(kl, ar = []) ⇒ ArrayOf
Create a new ArrayOf whose elements are supposed to be all of type kl, optionally filling it with the elements from the Array argument.
351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/rbot/irc.rb', line 351 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 |
Instance Attribute Details
#element_class ⇒ Object (readonly)
Returns the value of attribute element_class.
346 347 348 |
# File 'lib/rbot/irc.rb', line 346 def element_class @element_class end |
Instance Method Details
#&(ar) ⇒ Object
Overloaded from Array#&, checks for appropriate class of argument elements
410 411 412 413 |
# File 'lib/rbot/irc.rb', line 410 def &(ar) r = super(ar) ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r) end |
#+(ar) ⇒ Object
Overloaded from Array#+, checks for appropriate class of argument elements
417 418 419 |
# File 'lib/rbot/irc.rb', line 417 def +(ar) ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar) end |
#-(ar) ⇒ Object
Overloaded from Array#-, so that an ArrayOf is returned. There is no need to check the validity of the elements in the argument
424 425 426 |
# File 'lib/rbot/irc.rb', line 424 def -(ar) ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar) end |
#<<(el) ⇒ Object
Overloaded from Array#<<, checks for appropriate class of argument
404 405 406 |
# File 'lib/rbot/irc.rb', line 404 def <<(el) super(el) if internal_will_accept?(true, el) end |
#concat(ar) ⇒ Object
Overloaded from Array#concat, checks for appropriate class of argument elements
437 438 439 |
# File 'lib/rbot/irc.rb', line 437 def concat(ar) super(ar) if internal_will_accept?(true, *ar) end |
#downcase ⇒ Object
We introduce the ‘downcase’ method, which maps downcase() to all the Array elements, properly failing when the elements don’t have a downcase method
473 474 475 |
# File 'lib/rbot/irc.rb', line 473 def downcase self.map { |el| el.downcase } end |
#insert(idx, *ar) ⇒ Object
Overloaded from Array#insert, checks for appropriate class of argument elements
444 445 446 |
# File 'lib/rbot/irc.rb', line 444 def insert(idx, *ar) super(idx, *ar) if internal_will_accept?(true, *ar) end |
#inspect ⇒ Object
363 364 365 |
# File 'lib/rbot/irc.rb', line 363 def inspect self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>" end |
#push(*ar) ⇒ Object
Overloaded from Array#push, checks for appropriate class of argument elements
458 459 460 |
# File 'lib/rbot/irc.rb', line 458 def push(*ar) super(*ar) if internal_will_accept?(true, *ar) end |
#replace(ar) ⇒ Object
Overloaded from Array#replace, checks for appropriate class of argument elements
451 452 453 |
# File 'lib/rbot/irc.rb', line 451 def replace(ar) super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar) end |
#unshift(*els) ⇒ Object
Overloaded from Array#unshift, checks for appropriate class of argument(s)
464 465 466 467 468 |
# File 'lib/rbot/irc.rb', line 464 def unshift(*els) els.each { |el| super(el) if internal_will_accept?(true, *els) } end |
#valid? ⇒ Boolean
This method checks that all elements are of the appropriate class
391 392 393 |
# File 'lib/rbot/irc.rb', line 391 def valid? will_accept?(*self) end |
#validate ⇒ Object
This method is similar to the above, except that it raises an exception if the receiver is not valid
398 399 400 |
# File 'lib/rbot/irc.rb', line 398 def validate raise TypeError unless valid? end |
#will_accept?(*els) ⇒ Boolean
This method checks if the passed arguments are acceptable for our ArrayOf
385 386 387 |
# File 'lib/rbot/irc.rb', line 385 def will_accept?(*els) internal_will_accept?(false, *els) end |