7stud2
(7stud --)
15 February 2014 12:54
1
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:
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:
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/ .
abinoam
(Abinoam Praxedes Marques Jr.)
15 February 2014 13:21
2
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:
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:
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/\ .
7stud2
(7stud --)
15 February 2014 13:32
3
Hehe, so simple and logical but I would never think about that
Thank you dear Abinoam Jr.
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
15 February 2014 14:09
4
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/ .
abinoam
(Abinoam Praxedes Marques Jr.)
15 February 2014 13:42
5
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/\ .
abinoam
(Abinoam Praxedes Marques Jr.)
15 February 2014 15:02
6
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/\ .