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)
- 
  
    
      - (Object) element_class 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute element_class. 
Instance Method Summary (collapse)
- 
  
    
      - (Object) &(ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#&, checks for appropriate class of argument elements. 
- 
  
    
      - (Object) +(ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#+, checks for appropriate class of argument elements. 
- 
  
    
      - (Object) -(ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#-, so that an ArrayOf is returned. 
- 
  
    
      - (Object) <<(el) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#<<, checks for appropriate class of argument. 
- 
  
    
      - (Object) concat(ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#concat, checks for appropriate class of argument elements. 
- 
  
    
      - (Object) downcase 
    
    
  
  
  
  
  
  
  
  
  
    We introduce the 'downcase' method, which maps downcase() to all the Array elements, properly failing when the elements don't have a downcase method. 
- 
  
    
      - (ArrayOf) initialize(kl, ar = []) 
    
    
  
  
  
    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. 
- 
  
    
      - (Object) insert(idx, *ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#insert, checks for appropriate class of argument elements. 
- - (Object) inspect
- 
  
    
      - (Object) push(*ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#push, checks for appropriate class of argument elements. 
- 
  
    
      - (Object) replace(ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#replace, checks for appropriate class of argument elements. 
- 
  
    
      - (Object) unshift(*els) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#unshift, checks for appropriate class of argument(s). 
- 
  
    
      - (Boolean) valid? 
    
    
  
  
  
  
  
  
  
  
  
    This method checks that all elements are of the appropriate class. 
- 
  
    
      - (Object) validate 
    
    
  
  
  
  
  
  
  
  
  
    This method is similar to the above, except that it raises an exception if the receiver is not valid. 
- 
  
    
      - (Boolean) will_accept?(*els) 
    
    
  
  
  
  
  
  
  
  
  
    This method checks if the passed arguments are acceptable for our ArrayOf. 
- 
  
    
      - (Object) |(ar) 
    
    
  
  
  
  
  
  
  
  
  
    Overloaded from Array#|, checks for appropriate class of argument elements. 
Methods inherited from Array
#delete_if_at, #delete_one, #pick_one, #shuffle, #shuffle!
Constructor Details
- (ArrayOf) initialize(kl, ar = [])
Create a new ArrayOf whose elements are supposed to be all of type kl, optionally filling it with the elements from the Array argument.
| 374 375 376 377 378 379 380 381 382 383 384 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 374 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
- (Object) element_class (readonly)
Returns the value of attribute element_class
| 369 370 371 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 369 def element_class @element_class end | 
Instance Method Details
- (Object) &(ar)
Overloaded from Array#&, checks for appropriate class of argument elements
| 433 434 435 436 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 433 def &(ar) r = super(ar) ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r) end | 
- (Object) +(ar)
Overloaded from Array#+, checks for appropriate class of argument elements
| 440 441 442 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 440 def +(ar) ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar) end | 
- (Object) -(ar)
Overloaded from Array#-, so that an ArrayOf is returned. There is no need to check the validity of the elements in the argument
| 447 448 449 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 447 def -(ar) ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar) end | 
- (Object) <<(el)
Overloaded from Array#<<, checks for appropriate class of argument
| 427 428 429 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 427 def <<(el) super(el) if internal_will_accept?(true, el) end | 
- (Object) concat(ar)
Overloaded from Array#concat, checks for appropriate class of argument elements
| 460 461 462 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 460 def concat(ar) super(ar) if internal_will_accept?(true, *ar) end | 
- (Object) downcase
We introduce the 'downcase' method, which maps downcase() to all the Array elements, properly failing when the elements don't have a downcase method
| 496 497 498 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 496 def downcase self.map { |el| el.downcase } end | 
- (Object) insert(idx, *ar)
Overloaded from Array#insert, checks for appropriate class of argument elements
| 467 468 469 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 467 def insert(idx, *ar) super(idx, *ar) if internal_will_accept?(true, *ar) end | 
- (Object) inspect
| 386 387 388 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 386 def inspect self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>" end | 
- (Object) push(*ar)
Overloaded from Array#push, checks for appropriate class of argument elements
| 481 482 483 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 481 def push(*ar) super(*ar) if internal_will_accept?(true, *ar) end | 
- (Object) replace(ar)
Overloaded from Array#replace, checks for appropriate class of argument elements
| 474 475 476 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 474 def replace(ar) super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar) end | 
- (Object) unshift(*els)
Overloaded from Array#unshift, checks for appropriate class of argument(s)
| 487 488 489 490 491 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 487 def unshift(*els) els.each { |el| super(el) if internal_will_accept?(true, *els) } end | 
- (Boolean) valid?
This method checks that all elements are of the appropriate class
| 414 415 416 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 414 def valid? will_accept?(*self) end | 
- (Object) validate
This method is similar to the above, except that it raises an exception if the receiver is not valid
| 421 422 423 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 421 def validate raise TypeError unless valid? end | 
- (Boolean) will_accept?(*els)
This method checks if the passed arguments are acceptable for our ArrayOf
| 408 409 410 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 408 def will_accept?(*els) internal_will_accept?(false, *els) end | 
- (Object) |(ar)
Overloaded from Array#|, checks for appropriate class of argument elements
| 453 454 455 | # File '/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb', line 453 def |(ar) ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar) end |