# Fized size record from a string

**URL:** <https://rubytalk.org/t/fized-size-record-from-a-string/52221>\
**Category:** ruby-talk\
**Created:** [10 March 2009 13:53 UTC](https://rubytalk.org/t/fized-size-record-from-a-string/52221 "2009-03-10T13:53:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucac81](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@lucac81](https://rubytalk.org/u/lucac81)\
**Post date:** [10 March 2009 13:53 UTC](https://rubytalk.org/t/fized-size-record-from-a-string/52221/1 "2009-03-10T13:53:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter1](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Tim\_Hunter1](https://rubytalk.org/u/Tim_Hunter1)\
**Post date:** [10 March 2009 14:03 UTC](https://rubytalk.org/t/fized-size-record-from-a-string/52221/2 "2009-03-10T14:03:32Z")

</div>

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)

Look at the String#% method:

irb(main):003:0\> "%-16s" % "123"  
=\> "123 "

This won't work if the original string is longer than 16, though. In  
that case you just a copy of the original string.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen2](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dkmd\_nielsen2](https://rubytalk.org/u/dkmd_nielsen2)\
**Post date:** [10 March 2009 14:08 UTC](https://rubytalk.org/t/fized-size-record-from-a-string/52221/3 "2009-03-10T14:08:08Z")

</div>

Simple, not very intelligent way could be

('string' + '-'\*30)[0..16]

> **···**
>
> On Mar 10, 8:51 am, 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

---

<div class="post-metadata">

**Author:** ![lucac81](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@lucac81](https://rubytalk.org/u/lucac81)\
**Post date:** [10 March 2009 14:08 UTC](https://rubytalk.org/t/fized-size-record-from-a-string/52221/4 "2009-03-10T14:08:10Z")

</div>

Uhmm I just found how to achieve this... using the ljust method of the  
String class...

&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;"hello".ljust(4) #=\> "hello"  
&nbsp;&nbsp;&nbsp;"hello".ljust(20) #=\> "hello "  
&nbsp;&nbsp;&nbsp;"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)

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [10 March 2009 14:15 UTC](https://rubytalk.org/t/fized-size-record-from-a-string/52221/5 "2009-03-10T14:15:46Z")

</div>

> 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
> 
> str = "too short"

=\> "too short"

> lng = "this one is big enough"

=\> "this one is big enough"

With Kernel#sprintf (which String#% calls)

> "%-16.16s"%str

=\> "too short "

> "%-16.16s"%lng

=\> "this one is big "

With String#ljust

> str.ljust(16)

=\> "too short "

> lng.ljust(16)

=\> "this one is big enough"

With String# (not what you want for short ones)

> str[0,16]

=\> "too short"

> lng[0,16]

=\> "this one is big "

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Mar 10, 2009, at 9:53 AM, lucac81 wrote:
