Sha256: 44a43b04f2032fd634f3d6724bca6806cae40e7ea47b6ff85b36be1ed7245563

Contents?: true

Size: 889 Bytes

Versions: 6

Compression:

Stored size: 889 Bytes

Contents

# encoding: utf-8
# Array
class Array
  # Convert array to list
  #
  # @example Change separator for values
  #
  #   input = %w(v1 v2 v3)
  #
  #   input.to_list(separator: ' | '
  #   => "v1" | "v2" | "v3"
  #
  # @example Change last separator for values
  #
  #   input = %w(v1 v2 v3)
  #
  #   input.to_list(last_separator: ' and '
  #   => "v1", "v2" and "v3"
  #
  # @example Change character which is placed around values
  #
  #   input = %w(v1 v2 v3)
  #
  #   input.to_list(around: "'")
  #   => 'v1', 'v2', 'v3'
  #
  # @return [String]
  #   A string representation of list
  def to_list(separator: ', ', last_separator: separator, around: '"')
    items = map { |l| format("#{around}%s#{around}", l) }

    return items.join(last_separator) if items.size <= 2

    result = items.slice(0..-2).join(separator) << last_separator
    result << items.last

    result
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.11.18 lib/fedux_org_stdlib/core_ext/array/list.rb
fedux_org-stdlib-0.11.17 lib/fedux_org_stdlib/core_ext/array/list.rb
fedux_org-stdlib-0.11.16 lib/fedux_org_stdlib/core_ext/array/list.rb
fedux_org-stdlib-0.11.15 lib/fedux_org_stdlib/core_ext/array/list.rb
fedux_org-stdlib-0.11.14 lib/fedux_org_stdlib/core_ext/array/list.rb
fedux_org-stdlib-0.11.12 lib/fedux_org_stdlib/core_ext/array/list.rb