Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/fedux_org_stdlib/core_ext/array/list.rb

Overview

Array

Instance Method Summary collapse

Instance Method Details

#to_list(separator: ', ', last_separator: separator, around: '"') ⇒ String

Convert array to list

Examples:

Change separator for values


input = %w(v1 v2 v3)

input.to_list(separator: ' | '
=> "v1" | "v2" | "v3"

Change last separator for values


input = %w(v1 v2 v3)

input.to_list(last_separator: ' and '
=> "v1", "v2" and "v3"

Change character which is placed around values


input = %w(v1 v2 v3)

input.to_list(around: "'")
=> 'v1', 'v2', 'v3'

Returns:

  • (String)

    A string representation of list



29
30
31
32
33
34
35
36
37
38
# 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