How to get one time the ,

Hello,

I have this :

def solution(pairs)
pairs.each {|key, value| print “#{key} = #{value}” }
end

when I do solution({a: 1, b: ‘2’}) I see a = 1 b = 2
when I change the pairs to this

pairs.each {|key, value| print "#{key} = #{value}, " }

I will see a = 1, b=2,

is there a way I can take care that after the 2 no , is displayed.

Roelof

Try pushing the results into an array and then arr.join(",") perhaps?

Array#join will do the trick:

 print pairs.map { |key, value| "#{key} = #{value}" }.join(', ')

if you want a new-line at the end of that, then use puts instead of
print

unsubscribe

Thanks,

I just needed to use map instead of each.

Roelof

Wayne C. schreef op 6-6-2014 16:25:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs