Skip to content

Instantly share code, notes, and snippets.

@falonofthetower
Created January 15, 2015 05:57
Show Gist options
  • Save falonofthetower/a070637cca20c5a454e6 to your computer and use it in GitHub Desktop.
Save falonofthetower/a070637cca20c5a454e6 to your computer and use it in GitHub Desktop.
list = ([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
list2 = ([ {name: 'Bart'}, {name: 'Lisa'} ])
list3 = ([ {name: 'Bart'} ])
def transform set
names = ''
list_length = set.length
set.each_with_index do |name, index|
if index + 2 == list_length
name[:name].insert(-1, " & ")
elsif index + 1 != list_length
name[:name].insert(-1, ", ")
end
names << name[:name]
end
puts names
end
transform list
transform list2
transform list3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment