Confused in how to arrange data in Ruby

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.

On Fri, Mar 7, 2014 at 10:05 AM, Jaimin P. [email protected]
wrote:

|--------------------|--------|
| MATHEMATICS 89
| SOCIAL STUDIES 65

How do I get this?

Thank you very much.

http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-printf

Cheers

robert

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” [email protected] wrote on 03/07/2014
04:05:02
AM:

On Fri, Mar 7, 2014 at 11:13 AM, [email protected] 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.

Cheers

robert

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.

Robert K. wrote in post #1139125:

On Fri, Mar 7, 2014 at 11:13 AM, [email protected] 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.

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} |”

“ruby-talk” [email protected] wrote on 03/07/2014
07:41:17
AM:

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.

On Fri, Mar 7, 2014 at 1:41 PM, Jaimin P. [email protected]
wrote:

By using printf, spacing problem happen as I mentioned in my recent
post.

Please show the code.

Cheers

robert

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.

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 P. [email protected] wrote:

I used printf as follow:

Thank you.


Posted via http://www.ruby-forum.com/.

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Robert K. wrote in post #1139149:

On Fri, Mar 7, 2014 at 1:41 PM, Jaimin P. [email protected]
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.

On Fri, Mar 7, 2014 at 3:54 PM, Jaimin P. [email protected]
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

Hope this helps,

Mike

Excellent answer. It’s really help me.

Thank you very much.

Robert K. wrote in post #1139159:

On Fri, Mar 7, 2014 at 3:54 PM, Jaimin P. [email protected]
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.

On Fri, Mar 7, 2014 at 9:34 AM, Robert K.
[email protected] wrote:

I used printf as follow:

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/

Robert,

Perhaps (sf)printf formatted printing is going the way of regexp’s.
Soon no one will know how to format anything… :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs