Hi, how do I go about fully justifying/center justifying output text in
ruby?
I’m guessing I have to use sprintf but I can’t seem to get it working
properly, not sure of the correct “%” characters (don’t know the proper
name for them).
Hi, how do I go about fully justifying/center justifying output text in ruby?
I’m guessing I have to use sprintf but I can’t seem to get it working
properly, not sure of the correct “%” characters (don’t know the proper name
for them).
If you’re printing to terminal, you’ll need to know the terminal width
before use can use String#center. This is simple in Windows because
cmd.exe terminal is fixed-width. In Linux and OSX this is more
complicated. The easiest way I know is this:
require “curses”
width = Curses::cols
“hello”.center(width)
It’s not totally clear from your post if this is what you’re trying to
do, but if it is I hope this works you.
str.center(integer, padstr) => new_str
If you’re printing to terminal, you’ll need to know the terminal width
do, but if it is I hope this works you.
Yeah I think that’s what I want from what I can see.
Can’t get it to work for what I want though because the data is in an
array. Tried converting it to a string but can’t work that out either.
Not been doing ruby long as you might have guessed!
str.center(integer, padstr) => new_str
If you’re printing to terminal, you’ll need to know the terminal width
do, but if it is I hope this works you.
To do that without curses:
Can’t get it to work for what I want though because the data is in an
array. Tried converting it to a string but can’t work that out either.
Not been doing ruby long as you might have guessed!
It’s not totally clear from your post if this is what you’re trying to
do, but if it is I hope this works you.
Yeah I think that’s what I want from what I can see.
Can’t get it to work for what I want though because the data is in an
array. Tried converting it to a string but can’t work that out either.
Not been doing ruby long as you might have guessed!
Hi, how do I go about fully justifying/center justifying output text in
ruby?
I’m guessing I have to use sprintf but I can’t seem to get it working
properly, not sure of the correct “%” characters (don’t know the proper
name for them).
Check out Text::Format. It will even work with Text::Hyphen to
properly hyphenate your text.