diff options
author | Adam James <atj@pulsewidth.org.uk> | 2009-06-16 23:27:50 +0100 |
---|---|---|
committer | Adam James <atj@pulsewidth.org.uk> | 2009-06-29 23:23:33 +0100 |
commit | 561f183fb8c91867bade293c340a90a87a3b8c84 (patch) | |
tree | 93dc64d42837fe1aa78977c97d6c065ec8968d82 /lib/rbot | |
parent | dd1b52cacce13c8c66d210215c3cfe38356e67cf (diff) |
core/utils/extends.rb: add #conjoin for Array
Diffstat (limited to 'lib/rbot')
-rw-r--r-- | lib/rbot/core/utils/extends.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb index 083f2b85..332a1629 100644 --- a/lib/rbot/core/utils/extends.rb +++ b/lib/rbot/core/utils/extends.rb @@ -125,6 +125,45 @@ class ::Array end end + # This method is an advanced version of #join + # allowing fine control of separators: + # + # [1,2,3].conjoin(', ', ' and ') + # => "1, 2 and 3 + # + # [1,2,3,4].conjoin{ |i, a, b| i % 2 == 0 ? '.' : '-' } + # => "1.2-3.4" + # + # Code lifted from the ruby facets project: + # <http://facets.rubyforge.org> + # git-rev: c8b7395255b977d3c7de268ff563e3c5bc7f1441 + # file: lib/core/facets/array/conjoin.rb + def conjoin(*args, &block) + return first.to_s if size < 2 + + sep = [] + + if block_given? + (size - 1).times do |i| + sep << yield(i, *slice(i, 2)) + end + else + options = (Hash === args.last) ? args.pop : {} + separator = args.shift || "" + options[-1] = args.shift unless args.empty? + + sep = [separator] * (size - 1) + + if options.key?(:last) + options[-1] = options.delete(:last) + end + options[-1] ||= _(" and ") + + options.each{ |i, s| sep[i] = s } + end + + zip(sep).join + end end # Extensions to the Range class |