# Text formatting

**URL:** <https://rubytalk.org/t/text-formatting/69161>\
**Category:** ruby-talk\
**Created:** [15 February 2014 12:54 UTC](https://rubytalk.org/t/text-formatting/69161 "2014-02-15T12:54:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [15 February 2014 12:54 UTC](https://rubytalk.org/t/text-formatting/69161/1 "2014-02-15T12:54:34Z")

</div>

Hello people!  
I am very new to Ruby. Yesterday I bought very nice book about Ruby and  
immediately started learning. Now I have little problem 🙂  
The exercise is to display text like this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table of Contents

Chapter 1: Getting Started page 1  
Chapter 2: Numbers page 9  
Chapter 3: Letters page 13

but my text always looks like this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table of Contents

Chapter 1: Getting Started page 1  
Chapter 2: Numbers page 9  
Chapter 3: Letters page 13

My code is:

> **···**
>
> --------------------------------------------------------------------  
> line\_width = 50  
> str1 = 'Table of Contents'  
> str\_chap1 = 'Chapter 1: Getting Started'  
> str\_chap2 = 'Chapter 2: Numbers'  
> str\_chap3 = 'Chapter 3: Letters'
> 
> str\_page1 = 'page 1'  
> str\_page9 = 'page 9'  
> str\_page13 = 'page 13'
> 
> puts(str1.center(line\_width))  
> puts''  
> puts(str\_chap1.ljust(line\_width/2) + str\_page1.rjust(line\_width/2))  
> puts(str\_chap2.ljust(line\_width/2) + str\_page9.rjust(line\_width/2))  
> puts(str\_chap3.ljust(line\_width/2) + str\_page13.rjust(line\_width/2))  
> --------------------------------------------------------------------
> 
> What should I do to vertically align strings )'page 1', 'page 9' and  
> 'page 13')?
> 
> P.S. If you know some more better way to write this, I would be happy to  
> see it (and to try to understand it 🙂 )  
> P.S. Sory if this is a stuped question.
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)\
**Post date:** [15 February 2014 13:21 UTC](https://rubytalk.org/t/text-formatting/69161/2 "2014-02-15T13:21:46Z")

</div>

Dear "Keep Going",

puts str\_chap1.ljust(line\_width) + str\_page1  
puts str\_chap2.ljust(line\_width) + str\_page9  
puts str\_chap3.ljust(line\_width) + str\_page13

But the total line\_width will be line\_width (50) + str\_page.size.

If you want all the line to fit in the 50 chars you just need to  
subtract that value.

The longer line is str\_page13 = 'page 13' and is 7.  
So,

puts(str\_chap1.ljust(line\_width-7) + str\_page1)  
puts(str\_chap2.ljust(line\_width-7) + str\_page9)  
puts(str\_chap3.ljust(line\_width-7) + str\_page13)

Best regards,  
Abinoam Jr.

> **···**
>
> On Sat, Feb 15, 2014 at 9:54 AM, Keep Going \<lists@ruby-forum.com\> wrote:
> 
> > Hello people!  
> > I am very new to Ruby. Yesterday I bought very nice book about Ruby and  
> > immediately started learning. Now I have little problem 🙂  
> > The exercise is to display text like this:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table of Contents
> > 
> > Chapter 1: Getting Started page 1  
> > Chapter 2: Numbers page 9  
> > Chapter 3: Letters page 13
> > 
> > but my text always looks like this:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table of Contents
> > 
> > Chapter 1: Getting Started page 1  
> > Chapter 2: Numbers page 9  
> > Chapter 3: Letters page 13
> > 
> > My code is:  
> > --------------------------------------------------------------------  
> > line\_width = 50  
> > str1 = 'Table of Contents'  
> > str\_chap1 = 'Chapter 1: Getting Started'  
> > str\_chap2 = 'Chapter 2: Numbers'  
> > str\_chap3 = 'Chapter 3: Letters'
> > 
> > str\_page1 = 'page 1'  
> > str\_page9 = 'page 9'  
> > str\_page13 = 'page 13'
> > 
> > puts(str1.center(line\_width))  
> > puts''  
> > puts(str\_chap1.ljust(line\_width/2) + str\_page1.rjust(line\_width/2))  
> > puts(str\_chap2.ljust(line\_width/2) + str\_page9.rjust(line\_width/2))  
> > puts(str\_chap3.ljust(line\_width/2) + str\_page13.rjust(line\_width/2))  
> > --------------------------------------------------------------------
> > 
> > What should I do to vertically align strings )'page 1', 'page 9' and  
> > 'page 13')?
> > 
> > P.S. If you know some more better way to write this, I would be happy to  
> > see it (and to try to understand it 🙂 )  
> > P.S. Sory if this is a stuped question.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [15 February 2014 13:32 UTC](https://rubytalk.org/t/text-formatting/69161/3 "2014-02-15T13:32:16Z")

</div>

Hehe, so simple and logical but I would never think about that 🙂  
Thank you dear Abinoam Jr. 🙂

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

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [15 February 2014 14:09 UTC](https://rubytalk.org/t/text-formatting/69161/4 "2014-02-15T14:09:56Z")

</div>

Hmmm, I see almost everybody are using real names here so...  
Done & done!  
Now, I am using my real name 🙂

quote:  
So, "Keep Going" keep going and you will do it well!

Thank you 🙂

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

---

<div class="post-metadata">

**Author:** ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)\
**Post date:** [15 February 2014 13:42 UTC](https://rubytalk.org/t/text-formatting/69161/5 "2014-02-15T13:42:33Z")

</div>

You're welcome!

But... Abinoam in not a nick... is my real name 🙂

Abinoam Jr.

PS: Posting from ruby-forum you are showing up as "Keep Going" 🙂  
So, "Keep Going" keep going and you will do it well!

> **···**
>
> On Sat, Feb 15, 2014 at 10:32 AM, Keep Going \<lists@ruby-forum.com\> wrote:
> 
> > Hehe, so simple and logical but I would never think about that 🙂  
> > Thank you dear Abinoam Jr. 🙂
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)\
**Post date:** [15 February 2014 15:02 UTC](https://rubytalk.org/t/text-formatting/69161/6 "2014-02-15T15:02:58Z")

</div>

Correcting...

Vladimir Magoc, keep going and you will do it well!!! 😉

Best regards,  
Abinoam Jr.

> **···**
>
> On Sat, Feb 15, 2014 at 11:09 AM, Vladimir Magoc \<lists@ruby-forum.com\> wrote:
> 
> > Hmmm, I see almost everybody are using real names here so...  
> > Done & done!  
> > Now, I am using my real name 🙂
> > 
> > quote:  
> > So, "Keep Going" keep going and you will do it well!
> > 
> > Thank you 🙂
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
