From ab0c959bc7305d853e4a52b2a0f25a5fc78f4bfb Mon Sep 17 00:00:00 2001 From: Raine Virta Date: Tue, 3 Mar 2009 08:44:20 +0200 Subject: utils: add a helper method for creating comma separated lists --- lib/rbot/core/utils/utils.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib') 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 -- cgit v1.2.3