Is there a Ruby function that can pad a string?
Original [477, 4770]
Expected ["477 ", "4770 "]
You should use String#ljust from Ruby standard library:
arr = [477, 4770]
strings = arr.map { |number| number.to_s.ljust(5) }
# => ["477 ", "4770 "]
rjust(5) to add padding spaces to the left side of the string (at the risk of stating the obvious!) - apidock.com/ruby/String/rjustYou need the method String#ljust. Here is an example:
t = 123
s = t.to_s.ljust(5, ' ')
Please note that ' ' is the default padding symbol. I only added it for clarity.
You can use printf formatting from Kernel as well :
arr = [477, 4770]
arr.map { |i| "%-5d" % i }