class Array

Array

Public Instance Methods

to_list(separator: ', ', last_separator: separator, around: '"') click to toggle source

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
# File lib/fedux_org_stdlib/core_ext/array/list.rb, line 29
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