]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
plugin(search): fix search and gcalc, closes #28, #29
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index 208cb089a41b63fac593eaec6cbbc65093989ead..08278261fdfa435b21991a069cd66cb7664b9d31 100644 (file)
@@ -119,22 +119,22 @@ class ::Array
     end
   end
 
+  # Taken from Ruby backports:
   # shuffle and shuffle! are defined in Ruby >= 1.8.7
 
-  # This method returns a new array with the same items as
-  # the receiver, but shuffled
-  unless method_defined? :shuffle
-    def shuffle
-      sort_by { rand }
-    end
-  end
+  # This method returns a new array with the
+  # same items as the receiver, but shuffled
+  def shuffle
+    dup.shuffle!
+  end unless method_defined? :shuffle
 
-  # This method shuffles the items in the array
-  unless method_defined? :shuffle!
-    def shuffle!
-      replace shuffle
+  def shuffle!
+    size.times do |i|
+      r = i + Kernel.rand(size - i)
+      self[i], self[r] = self[r], self[i]
     end
-  end
+    self
+  end unless method_defined? :shuffle!
 end
 
 module ::Enumerable
@@ -370,8 +370,29 @@ class ::String
       "#{pre}#{self}#{post}"
     end
   end
-end
 
+  # Format a string using IRC colors
+  #
+  def colorformat
+    txt = self.dup
+
+    txt.gsub!(/\*([^\*]+)\*/, Bold + '\\1' + NormalText)
+
+    return txt
+  end
+  
+  # Removes non-ASCII symbols from string
+  def remove_nonascii(replace='')
+    encoding_options = {
+      :invalid           => :replace,  # Replace invalid byte sequences
+      :undef             => :replace,  # Replace anything not defined in ASCII
+      :replace           => replace,   
+      :universal_newline => true       # Always break lines with \n
+    }
+
+    self.encode(Encoding.find('ASCII'), encoding_options)
+  end
+end
 
 # Extensions to the Regexp class, with some common and/or complex regular
 # expressions.