]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
extends: bring conjoin to Enumerable
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 29 Jun 2009 22:41:58 +0000 (00:41 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 29 Jun 2009 22:41:58 +0000 (00:41 +0200)
Put #conjoin() in the Enumerable module, so that it can be shared by all
enumerables (e.g. ranges). Since #size() is not necessarily present, its
use is replaced by #count(), the result of which is cached because
it can be slow on objects missing #size().

lib/rbot/core/utils/extends.rb

index 332a16296473b529c2e58f306935de17e887458e..fc7119fa8c7df89d4bbf969ac88733e28d479635 100644 (file)
@@ -124,7 +124,9 @@ class ::Array
       replace shuffle
     end
   end
+end
 
+module ::Enumerable
   # This method is an advanced version of #join
   # allowing fine control of separators:
   #
@@ -139,12 +141,14 @@ class ::Array
   # git-rev: c8b7395255b977d3c7de268ff563e3c5bc7f1441
   # file: lib/core/facets/array/conjoin.rb
   def conjoin(*args, &block)
-    return first.to_s if size < 2
+    num = count - 1
+
+    return first.to_s if num < 1
 
     sep = []
 
     if block_given?
-      (size - 1).times do |i|
+      num.times do |i|
         sep << yield(i, *slice(i, 2))
       end
     else
@@ -152,7 +156,7 @@ class ::Array
       separator = args.shift || ""
       options[-1] = args.shift unless args.empty?
 
-      sep = [separator] * (size - 1)
+      sep = [separator] * num
 
       if options.key?(:last)
         options[-1] = options.delete(:last)