# Formatting text

**URL:** <https://rubytalk.org/t/formatting-text/39389>\
**Category:** ruby-talk\
**Created:** [25 July 2007 09:33 UTC](https://rubytalk.org/t/formatting-text/39389 "2007-07-25T09:33:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Tyman](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Marcin\_Tyman](https://rubytalk.org/u/Marcin_Tyman)\
**Post date:** [25 July 2007 09:33 UTC](https://rubytalk.org/t/formatting-text/39389/1 "2007-07-25T09:33:29Z")

</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  
&nbsp;&nbsp;&nbsp;1  
&nbsp;&nbsp;10  
1100

not like that:  
100  
1  
10  
1100

Any help would be helpful (as always 😉 )

thanks in advance

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 July 2007 09:37 UTC](https://rubytalk.org/t/formatting-text/39389/2 "2007-07-25T09:37:41Z")

</div>

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

> **···**
>
> 2007/7/25, Marcin Tyman \<m.tyman@interia.pl\>:
> 
> > 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  
> > &nbsp;&nbsp;&nbsp;1  
> > &nbsp;&nbsp;10  
> > 1100
> > 
> > not like that:  
> > 100  
> > 1  
> > 10  
> > 1100
> > 
> > Any help would be helpful (as always 😉 )

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [25 July 2007 09:46 UTC](https://rubytalk.org/t/formatting-text/39389/3 "2007-07-25T09:46:40Z")

</div>

# 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)}  
&nbsp;&nbsp;&nbsp;1  
&nbsp;&nbsp;20  
333  
1000  
&nbsp;&nbsp;55

this is just using #rjust.

irb(main):093:0\> system "qri String#rjust"  
----------------------------------------------------------- String#rjust  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.rjust(integer, padstr=' ') =\> new\_str

> **···**
>
> From: Marcin Tyman [[mailto:m.tyman@interia.pl](mailto:m.tyman@interia.pl)]  
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If integer is greater than the length of str, returns a new String  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of length integer with str right justified and padded with padstr;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;otherwise, returns str.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hello".rjust(4) #=\> "hello"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hello".rjust(20) #=\> " hello"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"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:** ![Marcin\_Tyman](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Marcin\_Tyman](https://rubytalk.org/u/Marcin_Tyman)\
**Post date:** [25 July 2007 09:49 UTC](https://rubytalk.org/t/formatting-text/39389/4 "2007-07-25T09:49:19Z")

</div>

Robert Klemme wrote:

> **···**
>
> > 2007/7/25, Marcin Tyman \<m.tyman@interia.pl\>:
> > 
> > > 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).  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [25 July 2007 09:54 UTC](https://rubytalk.org/t/formatting-text/39389/5 "2007-07-25T09:54:26Z")

</div>

Marcin Tyman wrote:

> Robert Klemme 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.

> **···**
>
> --  
> NP: Katatonia - Fractured  
> Ist so, weil ist so  
> Bleibt so, weil war so

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 July 2007 09:54 UTC](https://rubytalk.org/t/formatting-text/39389/6 "2007-07-25T09:54:41Z")

</div>

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

> **···**
>
> 2007/7/25, Marcin Tyman \<m.tyman@interia.pl\>:
> 
> > Robert Klemme wrote:  
> > \> 2007/7/25, Marcin Tyman \<m.tyman@interia.pl\>:  
> > \>\> 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:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [25 July 2007 10:03 UTC](https://rubytalk.org/t/formatting-text/39389/7 "2007-07-25T10:03:14Z")

</div>

# 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

> **···**
>
> From: Marcin Tyman [[mailto:m.tyman@interia.pl](mailto:m.tyman@interia.pl)]

---

<div class="post-metadata">

**Author:** ![Kaldrenon](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Kaldrenon](https://rubytalk.org/u/Kaldrenon)\
**Post date:** [25 July 2007 13:05 UTC](https://rubytalk.org/t/formatting-text/39389/8 "2007-07-25T13:05:06Z")

</div>

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)

> **···**
>
> On Jul 25, 5:54 am, Sebastian Hungerecker \<sep...@googlemail.com\> 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.
