From: Raine Virta Date: Tue, 3 Mar 2009 06:44:20 +0000 (+0200) Subject: utils: add a helper method for creating comma separated lists X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=ab0c959bc7305d853e4a52b2a0f25a5fc78f4bfb;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git utils: add a helper method for creating comma separated lists --- diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb index 77bdeecf..f9912ebb 100644 --- a/lib/rbot/core/utils/utils.rb +++ b/lib/rbot/core/utils/utils.rb @@ -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