# Formatting text

**URL:** <https://www.ruby-forum.com/t/formatting-text/110326>\
**Category:** Ruby\
**Created:** [July 25, 2007, 11:32am UTC](https://www.ruby-forum.com/t/formatting-text/110326 "2007-07-25T11:32:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 11:32am UTC](https://www.ruby-forum.com/t/formatting-text/110326/1 "2007-07-25T11:32:57Z")

</div>

Hi again 🙂  
Is there any way to format a string. I.E. I’ve got a variable which may  
be 1, 2, 3 and the variable is added to string. I want to show whole  
string formatted as follows:  
100  
1  
10  
1100

not like that:  
100  
1  
10  
1100

Any help would be helpful (as always 😉 )

thanks in advance

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 11:38am UTC](https://www.ruby-forum.com/t/formatting-text/110326/2 "2007-07-25T11:38:28Z")

</div>

2007/7/25, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 100  
> 1  
> 10  
> 1100
> 
> Any help would be helpful (as always 😉 )

Please look for printf, sprintf and % in the docs. The last one might  
be hard to find. You use it as: “%3d” % 1

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 11:47am UTC](https://www.ruby-forum.com/t/formatting-text/110326/3 "2007-07-25T11:47:28Z")

</div>

From: Marcin T. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Is there any way to format a string. I.E. I’ve got a variable

# which may be 1, 2, 3 and the variable is added to string. I want

# to show whole string formatted as follows:

# 100

# 1

# 10

# 1100

it would be nice to show original string and it’s transformation,

but anyway, is it like this?

irb(main):092:0\> “1 20 333 1000 55”.split.each{|x| puts x.rjust(4)}  
1  
20  
333  
1000  
55

this is just using #rjust.

## irb(main):093:0\> system “qri String#rjust” ----------------------------------------------------------- String#rjust str.rjust(integer, padstr=’ ') =\> new\_str

```
 If integer is greater than the length of str, returns a new String
 of length integer with str right justified and padded with padstr;
 otherwise, returns str.

    "hello".rjust(4) #=> "hello"
    "hello".rjust(20) #=> " hello"
    "hello".rjust(20, '1234') #=> "123412341234123hello"

```

=\> true  
irb(main):094:0\>

robert already gave a handful of other hints.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 11:56am UTC](https://www.ruby-forum.com/t/formatting-text/110326/4 "2007-07-25T11:56:28Z")

</div>

Marcin T. wrote:

> Robert K. wrote:

> > Please look for printf, sprintf and % in the docs. The last one might  
> > be hard to find. You use it as: “%3d” % 1
> 
> OK, Fine. But… I want to format the string not its output to redirect  
> the string to a file on remote machine (the file should be formatted).

sprintf and % return a string, they don’t print anything to the screen.  
You  
can write the result of “%3d” % 1 to a file without a problem.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 11:49am UTC](https://www.ruby-forum.com/t/formatting-text/110326/5 "2007-07-25T11:49:18Z")

</div>

Robert K. wrote:

> 2007/7/25, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 100  
> > 1  
> > 10  
> > 1100
> > 
> > Any help would be helpful (as always 😉 )
> 
> Please look for printf, sprintf and % in the docs. The last one might  
> be hard to find. You use it as: “%3d” % 1
> 
> Kind regards
> 
> robert

OK, Fine. But… I want to format the string not its output to redirect  
the string to a file on remote machine (the file should be formatted).

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 11:57am UTC](https://www.ruby-forum.com/t/formatting-text/110326/6 "2007-07-25T11:57:33Z")

</div>

2007/7/25, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > be hard to find. You use it as: “%3d” % 1
> > 
> > Kind regards
> > 
> > robert
> 
> OK, Fine. But… I want to format the string not its output to redirect  
> the string to a file on remote machine (the file should be formatted).

Now what? Do you want to output or not? I do not understand your  
remark because with the methods I listed you can do output as well as  
change strings.

robert

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/formatting-text/110326/7 "2007-07-25T12:04:10Z")

</div>

From: Marcin T. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# OK, Fine. But… I want to format the string not its output

# to redirect the string to a file on remote machine (the file

# should be formatted).

the simple way is just do the output on screen (in a format you want).  
then if you’re happy with the output, just redirect it to a file.

like eg,

ruby mytestprogram.rb \> myfile

just try it.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 25, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/formatting-text/110326/8 "2007-07-25T15:06:24Z")

</div>

On Jul 25, 5:54 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> sprintf and % return a string, they don’t print anything to the screen. You  
> can write the result of “%3d” % 1 to a file without a problem.

Same is true for rjust() :  
[http://www.whytheluckystiff.net/ruby/pickaxe/html/ref\_c\_string.html#String.rjust](http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_string.html#String.rjust)
