Hi again
Is there any way to format a string. I.E. I’ve got a variable which may
be 1, 2, 3 and the variable is added to string. I want to show whole
string formatted as follows:
100
1
10
1100
not like that:
100
1
10
1100
Any help would be helpful (as always )
thanks in advance
2007/7/25, Marcin T. [email protected]:
100
1
10
1100
Any help would be helpful (as always )
Please look for printf, sprintf and % in the docs. The last one might
be hard to find. You use it as: “%3d” % 1
Kind regards
robert
From: Marcin T. [mailto:[email protected]]
Is there any way to format a string. I.E. I’ve got a variable
which may be 1, 2, 3 and the variable is added to string. I want
to show whole string formatted as follows:
100
1
10
1100
it would be nice to show original string and it’s transformation,
but anyway, is it like this?
irb(main):092:0> “1 20 333 1000 55”.split.each{|x| puts x.rjust(4)}
1
20
333
1000
55
this is just using #rjust.
irb(main):093:0> system “qri String#rjust”
----------------------------------------------------------- String#rjust
str.rjust(integer, padstr=’ ') => new_str
If integer is greater than the length of str, returns a new String
of length integer with str right justified and padded with padstr;
otherwise, returns str.
"hello".rjust(4) #=> "hello"
"hello".rjust(20) #=> " hello"
"hello".rjust(20, '1234') #=> "123412341234123hello"
=> true
irb(main):094:0>
robert already gave a handful of other hints.
kind regards -botp
Marcin T. wrote:
Robert K. wrote:
Please look for printf, sprintf and % in the docs. The last one might
be hard to find. You use it as: “%3d” % 1
OK, Fine. But… I want to format the string not its output to redirect
the string to a file on remote machine (the file should be formatted).
sprintf and % return a string, they don’t print anything to the screen.
You
can write the result of “%3d” % 1 to a file without a problem.
Robert K. wrote:
2007/7/25, Marcin T. [email protected]:
100
1
10
1100
Any help would be helpful (as always )
Please look for printf, sprintf and % in the docs. The last one might
be hard to find. You use it as: “%3d” % 1
Kind regards
robert
OK, Fine. But… I want to format the string not its output to redirect
the string to a file on remote machine (the file should be formatted).
2007/7/25, Marcin T. [email protected]:
be hard to find. You use it as: “%3d” % 1
Kind regards
robert
OK, Fine. But… I want to format the string not its output to redirect
the string to a file on remote machine (the file should be formatted).
Now what? Do you want to output or not? I do not understand your
remark because with the methods I listed you can do output as well as
change strings.
robert
From: Marcin T. [mailto:[email protected]]
OK, Fine. But… I want to format the string not its output
to redirect the string to a file on remote machine (the file
should be formatted).
the simple way is just do the output on screen (in a format you want).
then if you’re happy with the output, just redirect it to a file.
like eg,
ruby mytestprogram.rb > myfile
just try it.
kind regards -botp
On Jul 25, 5:54 am, Sebastian H. [email protected]
wrote:
sprintf and % return a string, they don’t print anything to the screen. You
can write the result of “%3d” % 1 to a file without a problem.
Same is true for rjust() :
http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_string.html#String.rjust