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.
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!)
(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. )
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.
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 Hajdu wrote:
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.
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!)
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).
On Mon, Oct 14, 2013 at 3:00 AM, Greg Hajdu <lists@ruby-forum.com> wrote:
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.
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!)
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
···
On Mon, Oct 14, 2013 at 2:36 AM, Greg Hajdu <lists@ruby-forum.com> 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).