Justify | ljust

I’m trying to format my output but however I try it just doesn’t wanna
work. I started ruby a few weeks ago so it’s hard to explain, let me
just show it.

puts “#{m_counter} #{payment} #{interest} #{principal} #{balance}”

How do I put .ljust(10) to each of them?
I tried this to:

puts m_counter.ljust(10) + payment.ljust(15) + interest.ljust(20)… etc
But first of all, itt adds the values and with the ljust next to it it
gives me an error message of course.

So basically my question is how do I print out more than 1 variable with
methods next to them. (Sorry if I used the wrong definitions… still
new!) :smiley:

try :

puts
“#{m_counter.to_s.ljust(10)}#{payment.to_s.ljust(20)}#{interest.to_s.
ljust(15)}#{principal.to_s.ljust(15)}#{balance.to_s.ljust(20)}”

or

puts m_counter.to_s.ljust(10) + payment.to_s.ljust(20) + …


Apostolos Pantsiopoulos
Software Engineer

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear Greg,

(I started writing this earlier but see your question has been
answered meanwhile; I’ll send it anyway, as the extra information may
be helpful. :slight_smile: )

When a number such as m_counter is interpolated within a string, it
gets converted to a string representation if possible (using .to_s).
That is why your puts line works. The methods ljust() and rjust() are
called on strings, so one way to accomplish what you’re looking for
would be to do something like

puts m_counter.to_s.ljust(10) + ...

although I suspect you might be looking for rjust() not ljust(), as
numbers as often right-aligned (although this depends on whether
you’re working with decimal places).

A different, and arguably pleasanter, way of accomplishing this would
be to use a string-format approach. For this, you can specify a string
of formats and apply it to an array of values. e.g.

puts "%10s%10s%10s%10s%10s" %

[m_counter,payment,interest,principal,balance]

That is the equivalent of .rjust(10) for each; for .ljust(10), use
“%-10s” instead of “%10s”.

Two excellent links for further study of this approach are:

Peace,
tiredpixel

On 14/10/2013 01:00, Greg H. wrote:

with the ljust next to it it gives me an error message of course.

So basically my question is how do I print out more than 1 variable
with methods next to them. (Sorry if I used the wrong definitions…
still new!) :smiley:

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSWzk7AAoJEOFolTkanF7VVb8H/0ef08qW+rLW2itGW6+OzOaN
EbWwTirThBE/B18pbC2C1vN2294fa5PHuIGmegCUqxy7NSx//sLZ3GM01hOCKcbz
v+WKHRsU63Pt2l8rp4u9TjabNsJJSpjZffgoFxShmRg3A7BdIUsWEfaVmER/g6js
y0Ekt9T4fIHl+58cAUpUMDT/dbcf0X+ED3sArTvn/SSuNtH9tgMsvgUL9RSuWwxe
cIdvfjoy9OiWwkn1av6KSzf0HuNXVRtCaMLHEyg4yGPol79pPvO0KoKgrv/1xe5y
QBHt6sF0AqUhvjmBhIsQedf0vI4YOUZwhOC43V9AgYXhi175HAwQTqYFDDfnVIE=
=wk6w
-----END PGP SIGNATURE-----

Wow, thank you for the quick answer. First one works like a charm! :slight_smile:

On Mon, Oct 14, 2013 at 2:36 AM, Greg H. [email protected]
wrote:

Thank you, I’m going to check the link out.
Actually I just realized that I was thinking that the + sign gonna add
them and didn’t realize that using + with strings just simply puts them
next to each other (what I was looking for). :smiley:

Since nobody mentioned printf I do. Assuming that all these are
integers:

printf “%-10d%-10d%-10d%-10d%-10d\n”, m_counter, payment, interest,
principal, balance

Note that you need the “-” to get left justification. If you want to
have right justification (which is more common with numbers) just omit
the dashs.

Kind regards

robert

Robert K. wrote in post #1124462:

printf “%-10d%-10d%-10d%-10d%-10d\n”, m_counter, payment, interest,
principal, balance

Just an additional demo with a DRY approach:

printf ("%-10d" * 5 + “\n”), *(1…5)

You can change that “5” to however many numbers you’re displaying.

On Mon, Oct 14, 2013 at 10:58 AM, Joel P. [email protected]
wrote:

Robert K. wrote in post #1124462:

printf “%-10d%-10d%-10d%-10d%-10d\n”, m_counter, payment, interest,
principal, balance

Just an additional demo with a DRY approach:

printf (“%-10d” * 5 + “\n”), *(1…5)

You can change that “5” to however many numbers you’re displaying.

You could generalize that approach

def output(values)
printf (“%-10d” * values.size + “\n”), *values
end

There is a runtime penalty though. Your solution could be saved with a
constant

FMT = (“%-10d” * 5 + “\n”).freeze

printf FMT, *(1…5)

But the “dynamic” one cannot - unless one starts using an Array or
Hash for storage of format strings… :slight_smile:

Kind regards

robert

A Hash sounds like a good way of improving the performance of a regular
calculation like that. I’m no expert on the efficiency of the C code
though.

Would this work?

FMT = Hash.new { |h,k| h[k] = “%-10d” * Integer( k ) + “\n” }

def output( values )
printf FMT[ values.size ], *values
end

Thank you, I’m going to check the link out.
Actually I just realized that I was thinking that the + sign gonna add
them and didn’t realize that using + with strings just simply puts them
next to each other (what I was looking for). :smiley:

On Mon, Oct 14, 2013 at 2:35 PM, Joel P. [email protected]
wrote:

A Hash sounds like a good way of improving the performance of a regular
calculation like that. I’m no expert on the efficiency of the C code
though.

Would this work?

I think so. Just watch out for multiple threads. :slight_smile:

FMT = Hash.new { |h,k| h[k] = “%-10d” * Integer( k ) + “\n” }

def output( values )
printf FMT[ values.size ], *values
end

We could even do

FMT = Hash.new { |h,k| h[k] = “%-10d” * Integer( k ) + “\n” }

def FMT.format(*values)
printf(self[values.size], *values)
end

:slight_smile:

Cheers

robert

On Oct 14, 2013, at 10:11 AM, Robert K. [email protected]
wrote:

FMT = Hash.new { |h,k| h[k] = “%-10d” * Integer( k ) + “\n” }

def FMT.format(*values)
printf(self[values.size], *values)
end

OMG seriously deep arcane stuff! I love this. It’s starting to look like
Haskell.

Oh, now we’re getting into singleton classes. I applaud your skills,
good sir :slight_smile: