7stud2
(7stud --)
7 March 2014 09:05
1
I solve one program. I got the output in table form like below:
--------------------|--------|
MATHEMATICS 89|
SOCIAL STUDIES 65|
But this output is not arrange well.
I want output like:
--------------------|--------|
MATHEMATICS 89
SOCIAL STUDIES 65
How do I get this?
Thank you very much.
···
--
Posted via http://www.ruby-forum.com/\ .
s1 = 'first string'
n1 = 89
s2 = 'second'
n2 = 93
puts "| #{s1.ljust(20)} | #{n1} |"
puts "| #{s2.ljust(20)} | #{n2} |"
#ljust & #rjust work well for formatting, they pad with space by default.
···
"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 03/07/2014 04:05:02 AM:
From: Jaimin Pandya <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 03/07/2014 04:05 AM
Subject: confused in how to arrange data in Ruby
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>
I solve one program. I got the output in table form like below:
>--------------------|--------|
> MATHEMATICS 89|
> SOCIAL STUDIES 65|
But this output is not arrange well.
I want output like:
>--------------------|--------|
> MATHEMATICS 89
> SOCIAL STUDIES 65
How do I get this?
Thank you very much.
--
Posted via http://www.ruby-forum.com/\ .
Your code only works if numbers are always between 10 and 99, i.e. two
digits. I think printf is much easier.
Cheers
robert
···
On Fri, Mar 7, 2014 at 11:13 AM, <HANSEM1@nationwide.com> wrote:
s1 = 'first string'
n1 = 89
s2 = 'second'
n2 = 93
puts "| #{s1.ljust(20)} | #{n1} |"
puts "| #{s2.ljust(20)} | #{n2} |"
#ljust & #rjust work well for formatting, they pad with space by default.
--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/
7stud2
(7stud --)
7 March 2014 12:35
5
unknown wrote in post #1139120:
s1 = 'first string'
n1 = 89
s2 = 'second'
n2 = 93
puts "| #{s1.ljust(20)} | #{n1} |"
puts "| #{s2.ljust(20)} | #{n2} |"
#ljust & #rjust work well for formatting, they pad with space by
default.
s1 = 'first string'
n1 = 00
s2 = 'second'
n2 = 9
Then format of output chnge like:
first string | 0 |
second | 90 |
How can I solve this?
Thank you very much.
···
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
7 March 2014 12:41
6
Robert Klemme wrote in post #1139125:
···
On Fri, Mar 7, 2014 at 11:13 AM, <HANSEM1@nationwide.com> wrote:
s1 = 'first string'
n1 = 89
s2 = 'second'
n2 = 93
puts "| #{s1.ljust(20)} | #{n1} |"
puts "| #{s2.ljust(20)} | #{n2} |"
#ljust & #rjust work well for formatting, they pad with space by default.
Your code only works if numbers are always between 10 and 99, i.e. two
digits. I think printf is much easier.
Thank you for your answer because I got new option which gain my
knowledge.
By using printf, spacing problem happen as I mentioned in my recent
post.
--
Posted via http://www.ruby-forum.com/\ .
course = 'course name'
grade = 23
the following will format both the course and the grade (grades will be
right justified)
puts "| #{course.ljust(20)} | #{grade.to_s.rjust(3)} |"
the following is the printf version
printf "| %-20s | %3d |", course, grade
both will work, it's a matter of preference.
I prefer the first version because the variables show up where they will
be in the output rather than at the end as parameters. For this simple
line it doesn't make much difference.
To keep the 'puts' cleaner you may want to format before the puts, like:
course_formatted = course.ljust(20)
grade_formatted = grade.to_s.rjust(2)
puts "| #{course_formatted} | #{grade_formatted} |"
From: Jaimin Pandya <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 03/07/2014 07:41 AM
Subject: Re: confused in how to arrange data in Ruby
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>
Robert Klemme wrote in post #1139125:
>
>> s1 = 'first string'
>> n1 = 89
>> s2 = 'second'
>> n2 = 93
>>
>> puts "| #{s1.ljust(20)} | #{n1} |"
>> puts "| #{s2.ljust(20)} | #{n2} |"
>>
>> #ljust & #rjust work well for formatting, they pad with space by
default.
···
"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 03/07/2014 07:41:17 AM:
> On Fri, Mar 7, 2014 at 11:13 AM, <HANSEM1@nationwide.com> wrote:
>>
>
> Your code only works if numbers are always between 10 and 99, i.e. two
> digits. I think printf is much easier.
Thank you for your answer because I got new option which gain my
knowledge.
By using printf, spacing problem happen as I mentioned in my recent
post.
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
7 March 2014 13:48
8
unknown wrote in post #1139142:
course = 'course name'
grade = 23
the following will format both the course and the grade (grades will be
right justified)
puts "| #{course.ljust(20)} | #{grade.to_s.rjust(3)} |"
the following is the printf version
printf "| %-20s | %3d |", course, grade
both will work, it's a matter of preference.
I prefer the first version because the variables show up where they will
be in the output rather than at the end as parameters. For this simple
line it doesn't make much difference.
To keep the 'puts' cleaner you may want to format before the puts, like:
course_formatted = course.ljust(20)
grade_formatted = grade.to_s.rjust(2)
puts "| #{course_formatted} | #{grade_formatted} |"
Thank you very much.
I got answer from your answer and I gain more knowledge also.
···
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
7 March 2014 14:54
10
Robert Klemme wrote in post #1139149:
By using printf, spacing problem happen as I mentioned in my recent
post.
Please show the code.
I used printf as follow:
s1 = 'first string'
n1 = 00
s2 = 'string'
n2 = 26
printf "| #{s1.ljust(20)} | #{n1} |"
printf "| #{s2.ljust(20)} | #{n2} |"
then I got spacing problem.
Thank you.
···
On Fri, Mar 7, 2014 at 1:41 PM, Jaimin Pandya <lists@ruby-forum.com> > wrote:
--
Posted via http://www.ruby-forum.com/\ .
You should make sure you know why you are using printf and / or using interpolation. If either s1 or s2 contained printf escape sequences (for example %d) things would get interesting:
[1] pry(main)> s1 = "I'll have 10%"
=> "I'll have 10%"
[2] pry(main)> n1 = 00
=> 0
[3] pry(main)> s2 = 'string'
=> "string"
[4] pry(main)> n2 = 26
=> 26
[5] pry(main)> printf "| #{s1.ljust(20)} | #{n1} |"
ArgumentError: malformed format string - %|
from (pry):5:in `printf'
[6] pry(main)> printf "| #{s2.ljust(20)} | #{n2} |"
string | 26 |=> nil
You might want to consider just using printf:
[7] pry(main)> printf '| %-20s | %02d |', s1, n1
I'll have 10% | 00 |=> nil
[8] pry(main)> printf '| %-20s | %02d |', s2, n2
string | 26 |=> nil
Hope this helps,
Mike
···
On Mar 7, 2014, at 9:54 AM, Jaimin Pandya <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1139149:
On Fri, Mar 7, 2014 at 1:41 PM, Jaimin Pandya <lists@ruby-forum.com> >> wrote:
By using printf, spacing problem happen as I mentioned in my recent
post.
Please show the code.
I used printf as follow:
s1 = 'first string'
n1 = 00
s2 = 'string'
n2 = 26
printf "| #{s1.ljust(20)} | #{n1} |"
printf "| #{s2.ljust(20)} | #{n2} |"
then I got spacing problem.
Thank you.
--
Posted via http://www.ruby-forum.com/\ .
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
No surprise. Apparently you did not actually _read_ the documentation
about the method.
robert
···
On Fri, Mar 7, 2014 at 3:54 PM, Jaimin Pandya <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1139149:
On Fri, Mar 7, 2014 at 1:41 PM, Jaimin Pandya <lists@ruby-forum.com> >> wrote:
By using printf, spacing problem happen as I mentioned in my recent
post.
Please show the code.
I used printf as follow:
s1 = 'first string'
n1 = 00
s2 = 'string'
n2 = 26
printf "| #{s1.ljust(20)} | #{n1} |"
printf "| #{s2.ljust(20)} | #{n2} |"
then I got spacing problem.
--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/
7stud2
(7stud --)
7 March 2014 15:40
13
Hope this helps,
Mike
Excellent answer. It's really help me.
Thank you very much.
···
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
7 March 2014 15:41
14
Robert Klemme wrote in post #1139159:
···
On Fri, Mar 7, 2014 at 3:54 PM, Jaimin Pandya <lists@ruby-forum.com> > wrote:
s1 = 'first string'
n1 = 00
s2 = 'string'
n2 = 26
printf "| #{s1.ljust(20)} | #{n1} |"
printf "| #{s2.ljust(20)} | #{n2} |"
then I got spacing problem.
No surprise. Apparently you did not actually _read_ the documentation
about the method.
robert
Yes, Now I understand.
Thank you.
--
Posted via http://www.ruby-forum.com/\ .
Robert,
Perhaps (sf)printf formatted printing is going the way of regexp's.
Soon no one will know how to format anything....
···
On Fri, Mar 7, 2014 at 9:34 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Fri, Mar 7, 2014 at 3:54 PM, Jaimin Pandya <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1139149:
On Fri, Mar 7, 2014 at 1:41 PM, Jaimin Pandya <lists@ruby-forum.com> >>> wrote:
By using printf, spacing problem happen as I mentioned in my recent
post.
Please show the code.
I used printf as follow:
s1 = 'first string'
n1 = 00
s2 = 'string'
n2 = 26
printf "| #{s1.ljust(20)} | #{n1} |"
printf "| #{s2.ljust(20)} | #{n2} |"
then I got spacing problem.
No surprise. Apparently you did not actually _read_ the documentation
about the method.
robert
--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/