format a string of names like ‘bart, lisa & maggie’.

Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:

1
2
3
4
5
6
7
8
9
10
11
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])


list([ {name: 'Bart'}, {name: 'Lisa'} ])
# returns 'Bart & Lisa'

list([ {name: 'Bart'} ])
# returns 'Bart'

list([])
# returns ''

Answers:

1
2
3
4
#大佬们的回答
def (names)
names.map(&:values).join(', ').reverse.sub(/,/, '& ').reverse
end