Ruby beginner:
Outputting a list using
each do
When I print the list, It iterates the puts"," the same amount of times as the list, because, well that's what I told it to do. How to I tell it to not to put the comma the last time/skip the last comma and put a question mark instead? Sample code below:
contacts = {
"Freddy" => {
"phone" => 666,
"tiktok" => "asdf",
"instagram" => "def",
"email" => "in.your@dreams"
}
}
name = "Freddy"
print "We know these things about #{name}:"
contacts[name].each do |key, value|
print " #{key}"
print ","
end
print "?\n"
Desired output is: We know these things about Freddy: phone, tictok, instagram, email?
not
We know these things about Freddy: phone, tictok, instagram, email,? (get rid of last comma)