]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Improve robustness of ArrayOf; fix some inspect methods
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Fri, 4 Aug 2006 14:49:36 +0000 (14:49 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Fri, 4 Aug 2006 14:49:36 +0000 (14:49 +0000)
lib/rbot/irc.rb

index 99ea2ebdf4833070c252884d563bf9dd14122754..cc446bd4584500f6a2c5fa9cd00ae3d7f2680e28 100644 (file)
@@ -161,12 +161,16 @@ class ArrayOf < Array
     @element_class = kl\r
     case ar\r
     when Array\r
-      send(:+, ar)\r
+      insert(0, *ar)\r
     else\r
       raise TypeError, "#{self.class} can only be initialized from an Array"\r
     end\r
   end\r
 \r
+  def inspect\r
+    "#<#{self.class}[#{@element_class}]:#{'0x%x' % self.object_id}: #{super}>"\r
+  end\r
+\r
   # Private method to check the validity of the elements passed to it\r
   # and optionally raise an error\r
   #\r
@@ -207,6 +211,60 @@ class ArrayOf < Array
     super(el) if internal_will_accept?(true, el)\r
   end\r
 \r
+  # Overloaded from Array#&, checks for appropriate class of argument elements\r
+  #\r
+  def &(ar)\r
+    r = super(ar)\r
+    ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r)\r
+  end\r
+\r
+  # Overloaded from Array#+, checks for appropriate class of argument elements\r
+  #\r
+  def +(ar)\r
+    ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)\r
+  end\r
+\r
+  # Overloaded from Array#-, so that an ArrayOf is returned. There is no need\r
+  # to check the validity of the elements in the argument\r
+  #\r
+  def -(ar)\r
+    ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar)\r
+  end\r
+\r
+  # Overloaded from Array#|, checks for appropriate class of argument elements\r
+  #\r
+  def |(ar)\r
+    ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)\r
+  end\r
+\r
+  # Overloaded from Array#concat, checks for appropriate class of argument\r
+  # elements\r
+  #\r
+  def concat(ar)\r
+    super(ar) if internal_will_accept?(true, *ar)\r
+  end\r
+\r
+  # Overloaded from Array#insert, checks for appropriate class of argument\r
+  # elements\r
+  #\r
+  def insert(idx, *ar)\r
+    super(idx, *ar) if internal_will_accept?(true, *ar)\r
+  end\r
+\r
+  # Overloaded from Array#replace, checks for appropriate class of argument\r
+  # elements\r
+  #\r
+  def replace(ar)\r
+    super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar)\r
+  end\r
+\r
+  # Overloaded from Array#push, checks for appropriate class of argument\r
+  # elements\r
+  #\r
+  def push(*ar)\r
+    super(*ar) if internal_will_accept?(true, *ar)\r
+  end\r
+\r
   # Overloaded from Array#unshift, checks for appropriate class of argument(s)\r
   #\r
   def unshift(*els)\r
@@ -215,11 +273,10 @@ class ArrayOf < Array
     }\r
   end\r
 \r
-  # Overloaded from Array#+, checks for appropriate class of argument elements\r
+  # Modifying methods which we don't handle yet are made private\r
   #\r
-  def +(ar)\r
-    super(ar) if internal_will_accept?(true, *ar)\r
-  end\r
+  private :[]=, :collect!, :map!, :fill, :flatten!\r
+\r
 end\r
 \r
 \r
@@ -304,9 +361,9 @@ module Irc
     end\r
 \r
     def inspect\r
-      str = "<#{self.class}:#{'0x%08x' % self.object_id}:"\r
+      str = "<#{self.class}:#{'0x%x' % self.object_id}:"\r
       str << " @nick=#{@nick.inspect} @user=#{@user.inspect}"\r
-      str << " @host=<#{@host}>"\r
+      str << " @host=#{@host.inspect}>"\r
       str\r
     end\r
 \r
@@ -647,10 +704,8 @@ module Irc
     attr_reader :name, :topic, :mode, :users, :server\r
     alias :to_s :name\r
 \r
-    # A String describing the Channel and (some of its) internals\r
-    #\r
     def inspect\r
-      str = "<#{self.class}:#{'0x%08x' % self.object_id}:"\r
+      str = "<#{self.class}:#{'0x%x' % self.object_id}:"\r
       str << " on server #{server}"\r
       str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"\r
       str << " @users=<#{@users.sort.join(', ')}>"\r
@@ -780,7 +835,7 @@ module Irc
         u.inspect\r
       }.sort\r
 \r
-      str = "<#{self.class}:#{'0x%08x' % self.object_id}:"\r
+      str = "<#{self.class}:#{'0x%x' % self.object_id}:"\r
       str << " @channels=#{chans}"\r
       str << " @users=#{users}>"\r
       str\r
@@ -1219,5 +1274,6 @@ module Irc
     end\r
 \r
   end\r
+\r
 end\r
 \r