I want my string in groups of 5 characters, .e.g.
thisisastring => ["thisi", "satri", "ng"]
but I also want the last group to be padded with __'s, e.g.
thisisastring => ["thisi", "satri", "ng___"]
I have got as far as the string splitting:
"thisisastring".scan /.{5}/)
["thisi", "satri", "ng"]
but not too sure how to do the padding for that last group to make it "ng___"
although starting to think that combinations of dividend (div()
), modulus (%
) and .ljust
might do it.
Maybe number of padding characters would be: (length % 5) * "_"
(if you can multiply that) ?
Perhaps something that uses:
ruby-1.9.2-p290 :023 > (len % 5).to_i.times { print '_' }
___ => 3
Since the adjustment is only required on the last element, it is more effective to do the adjustment before splitting rather than itterating over the elements to do adjustment.