]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
utils: add a helper method for creating comma separated lists
authorRaine Virta <rane@kapsi.fi>
Tue, 3 Mar 2009 06:44:20 +0000 (08:44 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 3 Mar 2009 19:52:12 +0000 (20:52 +0100)
lib/rbot/core/utils/utils.rb

index 77bdeecfb1cdeebc8a1759a622e148f5076b0be6..f9912ebb0b367b8e1e450b80b0e340644080ad44 100644 (file)
@@ -723,6 +723,20 @@ module ::Irc
       return retval
     end
 
+    # Returns a comma separated list except for the last element
+    # which is joined in with specified conjunction
+    #
+    def Utils.comma_list(words, options={})
+      defaults = { :join_with => ", ", :join_last_with => _(" and ") }
+      opts = defaults.merge(options)
+
+      if words.size < 2
+        words.last
+      else
+        [words[0..-2].join(opts[:join_with]), words.last].join(opts[:join_last_with])
+      end
+    end
+
   end
 end