robsite

Humanize Array

Kleine Funktion zum schönen ausgeben von Arrays.

# [1, 2, 3] => "1, 2 and 3"
# [1, 2]    => "1 and 2"
# [1]       => "1"
def humanize_array a
  [a[0..-3], [a[-2], a[-1]].compact.join(" and ")].flatten.join(", ")
end
· meins, ruby ·
Mastodon