Using sprintf() to print a Hash

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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
------------------------------------

I'm trying to play with sprintf by using %20s and so, with no success.
Any help please?

Thanks a lot.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
I

Iñaki Baz Castillo

2011/5/24 I=C3=B1aki Baz Castillo said:
------------------------------------
listen_ip: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A01.2.3.4
listen_port: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 90
use_tls: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tr= ue
compression: =C2=A0 =C2=A0 =C2=A0 =C2=A0 false

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
------------------------------------

but it's not exactly what I need.

Thanks a lot for any suggestion.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
I

Iñaki Baz Castillo

2011/5/24 I=C3=B1aki Baz Castillo said:
I've got the following output by using sprintf("%20s %20s",
"#{key.to_s}:", value.to_s):

------------------------------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0listen_ip: =C2=A0 =C2=A0 =C2=A0 1.2.3.4
=C2=A0 =C2=A0 listen_port: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A090
=C2=A0 =C2=A0 =C2=A0 =C2=A0 use_tls: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0true
=C2=A0compression: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 false

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
------------------------------------

:)



--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
X

Xavier Noria

[Note: parts of this message were removed to make it a legal post.]

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.
 
I

Iñaki Baz Castillo

2011/5/24 Xavier Noria said:
Not sure if I understand correctly, but this seems to do what you are
looking for

hash.each do |key, value|
=C2=A0printf "%-12s %10s\n", "#{key}:", value
end

Max 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.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
B

botp

... sprintf(" =A0%-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
 
I

Iñaki Baz Castillo

2011/5/24 Robert Klemme said:
That's overly complicated: you employ two text replacement mechanisms. = =C2=A0Just do

printf(" =C2=A0%-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.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
X

Xavier Noria

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top