Hi, I just have a single Hash and want to print it into the standar
output as follows (key: value):
listen_ip: 1.2.3.4
listen_port: 90
use_tls: true
compression: false
Hi, I just have a single Hash and want to print it into the standar
output as follows (key: value):
listen_ip: 1.2.3.4
listen_port: 90
use_tls: true
compression: false
2011/5/24 Iñaki Baz C. [email protected]:
listen_ip: 1.2.3.4
listen_port: 90
use_tls: true
compression: falseI’m trying to play with sprintf by using %20s and so, with no success.
Any help please?
I’ve got the following output by using sprintf(“%20s %20s”,
“#{key.to_s}:”, value.to_s):
listen_ip: 1.2.3.4
listen_port: 90
use_tls: true
compression: false
2011/5/24 Iñaki Baz C. [email protected]:
but it’s not exactly what I need.
Got it!:
sprintf(" %-20s %20s", “#{key.to_s}:”, value.to_s)
listen_ip: 1.2.3.4
listen_port: 90
use_tls: true
compression: false
2011/5/24 Xavier N. [email protected]:
Not sure if I understand correctly, but this seems to do what you are
looking forhash.each do |key, value|
printf “%-12s %10s\n”, “#{key}:”, value
endMax field widths may be computed for arbitrary hashes.
Yes, I got exactly that as todl in my previous mail
– fxn
PD: Note that I use printf, format is the same as sprintf, but you print the
formatted string directly, as in C.
Thanks for pointing it out.
Not sure if I understand correctly, but this seems to do what you are
looking for
hash.each do |key, value|
printf “%-12s %10s\n”, “#{key}:”, value
end
Max field widths may be computed for arbitrary hashes.
– fxn
PD: Note that I use printf, format is the same as sprintf, but you print
the
formatted string directly, as in C.
On Tue, May 24, 2011 at 7:59 PM, Iaki Baz C. [email protected]
wrote:
… sprintf(" %-20s %20s", “#{key.to_s}:”, value.to_s)
for simple formatting/justification, i just use the just methods since
they are easier (for my brain) to remember.
eg,
h.each do |k,v|
puts k.to_s.ljust(20) + v.to_s.rjust(20)
end
kind regards -botp
On Tue, May 24, 2011 at 1:59 PM, Iaki Baz C. [email protected]
wrote:
but it’s not exactly what I need.
Got it!:
sprintf(" %-20s %20s", “#{key.to_s}:”, value.to_s)
That’s overly complicated: you employ two text replacement mechanisms.
Just do
printf(" %-20s: %20s\n", key, value)
Cheers
robert
2011/5/24 botp [email protected]:
h.each do |k,v|
puts k.to_s.ljust(20) + v.to_s.rjust(20)
end
Great, I didn’t know about it
2011/5/24 Robert K. [email protected]:
sprintf(" %-20s %20s", “#{key.to_s}:”, value.to_s)
That’s overly complicated: you employ two text replacement mechanisms. Just do
printf(" %-20s: %20s\n", key, value)
Yes, initially I used key.to_s as I have Symbols as keys and thougth
that a string is required.
Changed now. Thanks.
On Tue, May 24, 2011 at 5:25 PM, Iñaki Baz C. [email protected]
wrote:
2011/5/24 Robert K. [email protected]:
sprintf(" %-20s %20s", “#{key.to_s}:”, value.to_s)
That’s overly complicated: you employ two text replacement mechanisms.
Just doprintf(" %-20s: %20s\n", key, value)
Yes, initially I used key.to_s as I have Symbols as keys and thougth
that a string is required.
Note that the colon belongs to the argument though. Otherwise they won’t
be
next to their keys.
2011/5/24 Xavier N. [email protected]:
Note that the colon belongs to the argument though. Otherwise they won’t be
next to their keys.
True.
On Tue, May 24, 2011 at 5:29 PM, Xavier N. [email protected] wrote:
Yes, initially I used key.to_s as I have Symbols as keys and thougth
that a string is required.Note that the colon belongs to the argument though. Otherwise they won’t be
next to their keys.
Right, I overlooked that. Good catch! I usually prefer to have all
colons aligned, that’s why I probably didn’t notice.
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs