yukimoda yukimoda - 2 months ago 21
Ruby Question

Ruby reverse histogram

So I'm new to ruby and while I was reading about histograms I had a thought. Is there was a way to have a histogram convert a number into a line of symbols? For example, 12 would be converted into ############.

Answer

You can multiply a string by an integer in Ruby.

value = 12
result = '#' * value
=> "############"

So for [1, 3, 5, 4] you could do

[1, 3, 5, 4].map{|value| '#' * value}
=> ["#", "###", "#####", "####"]
Comments