SuperManEver
SuperManEver

Reputation: 2362

How to make proper alignment with printf

I am trying to make output that should look like this:

enter image description here

I am using this statement:

FILE_TYPES.each_key {|key| printf "%20s %s\n", "--[no]" + key.to_s, FILE_TYPES[key.to_sym]}

Its result looks like this:

enter image description here

Upvotes: 1

Views: 64

Answers (2)

sawa
sawa

Reputation: 168209

If you do not insist on using printf,

puts "--[no]" + key.to_s.ljust(20) + FILE_TYPES[key.to_sym]

Upvotes: 3

Shahzad Tariq
Shahzad Tariq

Reputation: 2857

Use \t to give tab spaces for formatting strings. e.g.

printf "Name:\t Shahzad\nGender:\tMale\nAge:\t25\n"

and it would print result as

Name:    Shahzad
Gender: Male
Age:    25
=> nil 

Upvotes: 2

Related Questions