Class ArrayOf
In: lib/rbot/irc.rb
Parent: Array
User HTTPResponse BasicUserMessage Bot\n[lib/rbot/core/remote.rb\nlib/rbot/core/utils/extends.rb\nlib/rbot/core/utils/filters.rb\nlib/rbot/core/utils/wordlist.rb] HttpUtil BasicUserMessage JoinMessage NamesMessage WhoisMessage ModeChangeMessage KickMessage MotdMessage QuitMessage BanlistMessage UserMessage NoSuchTargetMessage TopicMessage NickMessage WelcomeMessage UnknownMessage InviteMessage PartMessage NetmaskList UserList ArrayOf ChannelList Netmask User\n[lib/rbot/botuser.rb\nlib/rbot/irc.rb] Channel Singleton RfcCasemap StrictRfcCasemap AsciiCasemap Casemap PrivMessage NoticeMessage TokyoCabinet::BDB CIBDB Btree CIBtree Socket MessageQueue QueueRing Client DBHash\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] DBTree\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] Server NetmaskDb Bot\n[lib/rbot/botuser.rb\nlib/rbot/config.rb\nlib/rbot/ircbot.rb\nlib/rbot/language.rb\nlib/rbot/message.rb\nlib/rbot/messagemapper.rb\nlib/rbot/plugins.rb\nlib/rbot/rbotconfig.rb\nlib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] String\n[lib/rbot/botuser.rb\nlib/rbot/core/utils/extends.rb\nlib/rbot/irc.rb\nlib/rbot/ircsocket.rb\nlib/rbot/load-gettext.rb] Regexp\n[lib/rbot/core/utils/extends.rb\nlib/rbot/irc.rb\nlib/rbot/messagemapper.rb] Array lib/rbot/core/userdata.rb lib/rbot/core/utils/httputil.rb lib/rbot/core/utils/extends.rb lib/rbot/core/remote.rb lib/rbot/core/utils/httputil.rb ParseTime Utils (null) lib/rbot/ircsocket.rb lib/rbot/rfc2812.rb lib/rbot/registry/tc.rb lib/rbot/irc.rb lib/rbot/maskdb.rb lib/rbot/message.rb lib/rbot/messagemapper.rb lib/rbot/botuser.rb lib/rbot/registry/tc.rb (null) BotConfig PKGConfig ServerOrCasemap Irc dot/f_2.png

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.

Methods

&   +   -   <<   concat   downcase   insert   inspect   internal_will_accept?   new   push   replace   unshift   valid?   validate   will_accept?   |  

Attributes

element_class  [R] 

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.

[Source]

     # 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

Public Instance methods

Overloaded from Array#&, checks for appropriate class of argument elements

[Source]

     # File lib/rbot/irc.rb, line 433
433:   def &(ar)
434:     r = super(ar)
435:     ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r)
436:   end

Overloaded from Array#+, checks for appropriate class of argument elements

[Source]

     # File lib/rbot/irc.rb, line 440
440:   def +(ar)
441:     ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
442:   end

Overloaded from Array#-, so that an ArrayOf is returned. There is no need to check the validity of the elements in the argument

[Source]

     # File lib/rbot/irc.rb, line 447
447:   def -(ar)
448:     ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar)
449:   end

Overloaded from Array#<<, checks for appropriate class of argument

[Source]

     # File lib/rbot/irc.rb, line 427
427:   def <<(el)
428:     super(el) if internal_will_accept?(true, el)
429:   end

Overloaded from Array#concat, checks for appropriate class of argument elements

[Source]

     # File lib/rbot/irc.rb, line 460
460:   def concat(ar)
461:     super(ar) if internal_will_accept?(true, *ar)
462:   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

[Source]

     # File lib/rbot/irc.rb, line 496
496:   def downcase
497:     self.map { |el| el.downcase }
498:   end

Overloaded from Array#insert, checks for appropriate class of argument elements

[Source]

     # File lib/rbot/irc.rb, line 467
467:   def insert(idx, *ar)
468:     super(idx, *ar) if internal_will_accept?(true, *ar)
469:   end

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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)

[Source]

     # 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

[Source]

     # 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

[Source]

     # File lib/rbot/irc.rb, line 421
421:   def validate
422:     raise TypeError unless valid?
423:   end

This method checks if the passed arguments are acceptable for our ArrayOf

[Source]

     # File lib/rbot/irc.rb, line 408
408:   def will_accept?(*els)
409:     internal_will_accept?(false, *els)
410:   end

Overloaded from Array#|, checks for appropriate class of argument elements

[Source]

     # File lib/rbot/irc.rb, line 453
453:   def |(ar)
454:     ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
455:   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?

[Source]

     # 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

[Validate]