lucac81
3/10/2009 2:07:00 PM
On Mar 10, 2:51 pm, lucac81 <lcors...@gmail.com> 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
Uhmm I just found how to achieve this... using the ljust method of the
String class...
str.ljust(integer, padstr=' ') => new_str
If integer is greater than the length of str, returns a new String of
length integer with str left justified and padded with padstr;
otherwise, returns str.
"hello".ljust(4) #=> "hello"
"hello".ljust(20) #=> "hello "
"hello".ljust(20, '1234') #=> "hello123412341234123"
this solves my problem, and there is the rjust method that works fine
on the other side (I need that too)