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
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`
···
On 06/06/2014 07:12 AM, Roelof Wobben wrote:
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?
···
On 6 June 2014 15:12, Roelof Wobben <r.wobben@home.nl> wrote:
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
--
== People often come up to me and ask "What the heck are you doing in my
shed!?" ==
Thanks,
I just needed to use map instead of each.
Roelof
Wayne Conrad schreef op 6-6-2014 16:25:
···
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`
On 06/06/2014 07:12 AM, Roelof Wobben wrote:
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