Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > fized size record from a string

Reply
Thread Tools

fized size record from a string

 
 
lucac81
Guest
Posts: n/a
 
      03-10-2009
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
 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      03-10-2009
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/.

 
Reply With Quote
 
 
 
 
dkmd_nielsen
Guest
Posts: n/a
 
      03-10-2009
On Mar 10, 8:51*am, lucac81 <(E-Mail Removed)> 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


Simple, not very intelligent way could be

('string' + '-'*30)[0..16]
 
Reply With Quote
 
lucac81
Guest
Posts: n/a
 
      03-10-2009
On Mar 10, 2:51*pm, lucac81 <(E-Mail Removed)> 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)
 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      03-10-2009

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
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
You cannot add or change a record because a related record is required in table 'lok" André ASP .Net 0 06-25-2006 01:30 PM
Retrieving Record Key while creating the record. =?Utf-8?B?SnVzdGlu?= ASP .Net 4 10-05-2004 08:11 PM
" Invalid Disk Table in Boot Record - Boot Record could not be repaired " reply@newsgroup.please Computer Support 2 12-01-2003 05:37 AM



Advertisments