From: Rob Biedenharn Date: 2009-03-10T23:15:46+09:00 Subject: Re: fized size record from a string On Mar 10, 2009, at 9:53 AM, lucac81 wrote: > Hello, I'm new to Ruby, and I'm learning it with a project that also > involves rails... > I'm stuck with an apparently simple problem, I receive from a form a > String and I need to extend it to a fixed char number (it must be 16 > chars with eventual trailing spaces) > I've tought to convert it to an array, it's easy but I don't know how > to extend it to the needed size. > Also I could add spaces to the string, but the again how can i control > how many I need to add? > Any hint on how I could do that? > Thank you very much > irb> str = "too short" => "too short" irb> lng = "this one is big enough" => "this one is big enough" With Kernel#sprintf (which String#% calls) irb> "%-16.16s"%str => "too short " irb> "%-16.16s"%lng => "this one is big " With String#ljust irb> str.ljust(16) => "too short " irb> lng.ljust(16) => "this one is big enough" With String#[] (not what you want for short ones) irb> str[0,16] => "too short" irb> lng[0,16] => "this one is big " -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com