indent strings(make them lined up)

Discussion in 'Ruby' started by Jon Kim, Dec 8, 2009.

  1. Jon Kim

    Jon Kim Guest

    Hi,

    I am newbie in Ruby. I have a question about indenting string.

    there is strings like below:

    www.google.com => 123.123.123.12
    www.ibm.com => 123.112.123.12
    www.iowahawkeyes => 123.123.112.12

    i want to make above like next

    www.google.com => 123.123.123.12
    www.ibm.com => 123.112.123.12
    www.iowahawkeyes => 123.123.112.12


    original code was

    puts (dns + " => " + ip)

    I tried next code

    puts (dns + " "*(20 - dns.length) + " = > " + ip)

    but it did not work well. please teach me how to do it.
    --
    Posted via http://www.ruby-forum.com/.
    Jon Kim, Dec 8, 2009
    #1
    1. Advertising

  2. Jon Kim

    Intransition Guest

    On Dec 7, 11:26=A0pm, Jon Kim <> wrote:
    > Hi,
    >
    > I am newbie in Ruby. I have a question about indenting string.
    >
    > there is strings like below:
    >
    > www.google.com=3D> 123.123.123.12www.ibm.com=3D> 123.112.123.12www.iowaha=

    wkeyes=3D> 123.123.112.12
    >
    > i want to make above like next
    >
    > www.google.com=A0 =A0 =3D> 123.123.123.12www.ibm.com=A0 =A0 =A0 =A0=3D> 1=

    23.112.123.12www.iowahawkeyes=A0 =3D> 123.123.112.12
    >
    > original code was
    >
    > puts (dns + " =3D> " + ip)
    >
    > I tried next code
    >
    > puts (dns + " =A0"*(20 - dns.length) + " =3D > " + ip)
    >
    > but it did not work well. please teach me how to do it.


    "%-30s %s" % [dns, ip]
    Intransition, Dec 8, 2009
    #2
    1. Advertising

  3. Thomas Sawyer wrote:
    > On Dec 7, 11:26�pm, Jon Kim <> wrote:
    >> www.google.com� � => 123.123.123.12www.ibm.com� � � �=> 123.112.123.12www.iowahawkeyes� => 123.123.112.12
    >>
    >> original code was
    >>
    >> puts (dns + " => " + ip)
    >>
    >> I tried next code
    >>
    >> puts (dns + " �"*(20 - dns.length) + " = > " + ip)
    >>
    >> but it did not work well. please teach me how to do it.

    >
    > "%-30s %s" % [dns, ip]


    Thomas's suggestion of String Format (%) is correct, just revising
    example to reflect the original code above.

    puts "%-20s => %s" % [dns, ip]
    --
    Posted via http://www.ruby-forum.com/.
    Steve Wilhelm, Dec 8, 2009
    #3
  4. Jon Kim wrote:
    > Hi,
    >
    > I am newbie in Ruby. I have a question about indenting string.
    >
    > there is strings like below:
    >
    > www.google.com => 123.123.123.12
    > www.ibm.com => 123.112.123.12
    > www.iowahawkeyes => 123.123.112.12
    >
    > i want to make above like next
    >
    > www.google.com => 123.123.123.12
    > www.ibm.com => 123.112.123.12
    > www.iowahawkeyes => 123.123.112.12
    >
    >
    > original code was
    >
    > puts (dns + " => " + ip)
    >
    > I tried next code
    >
    > puts (dns + " "*(20 - dns.length) + " = > " + ip)
    >
    > but it did not work well. please teach me how to do it.
    >


    You could do

    puts "#{dns.ljust(20)} => #{ip}"


    -Justin
    Justin Collins, Dec 8, 2009
    #4
  5. On Dec 8, 2009, at 5:11 AM, Justin Collins wrote:
    > Jon Kim wrote:
    >> Hi,
    >>
    >> I am newbie in Ruby. I have a question about indenting string.
    >>
    >> there is strings like below:
    >>
    >> www.google.com => 123.123.123.12
    >> www.ibm.com => 123.112.123.12
    >> www.iowahawkeyes => 123.123.112.12
    >>
    >> i want to make above like next
    >>
    >> www.google.com => 123.123.123.12
    >> www.ibm.com => 123.112.123.12
    >> www.iowahawkeyes => 123.123.112.12
    >>
    >>
    >> original code was
    >>
    >> puts (dns + " => " + ip)
    >>
    >> I tried next code
    >>
    >> puts (dns + " "*(20 - dns.length) + " = > " + ip)
    >>
    >> but it did not work well. please teach me how to do it.
    >>

    >
    > You could do
    >
    > puts "#{dns.ljust(20)} => #{ip}"
    >
    > -Justin


    OK, I'm going to go out on a limb and presume that you obtain the
    successive dns and ip values from some sort of data structure. I'm
    going to further assume a hash since you're using the association
    syntax in your output string, but feel free to generalize the parts of
    this code that handle that iteration.

    irb> my_data = {
    ?> 'www.google.com' => '216.68.119.70',
    ?> 'www.ibm.com' => '129.42.60.216',
    ?> 'www.iowahawkeyes.com' => '199.108.163.135',
    ?> }
    => {"www.google.com"=>"216.68.119.70", "www.ibm.com"=>"129.42.60.216",
    "www.iowahawkeyes.com"=>"199.108.163.135"}
    irb> width = my_data.keys.inject(0) {|wmax,dns| [dns.length, wmax].max }
    => 20
    irb> my_data.each do |dns,ip|
    ?> puts "%-*s => %s"%[width, dns, ip]
    irb> end
    www.google.com => 216.68.119.70
    www.ibm.com => 129.42.60.216
    www.iowahawkeyes.com => 199.108.163.135
    => {"www.google.com"=>"216.68.119.70", "www.ibm.com"=>"129.42.60.216",
    "www.iowahawkeyes.com"=>"199.108.163.135"}
    irb>
    ?> my_data['www.letmegooglethatforyou.com'] = '209.20.88.2'
    => "209.20.88.2"
    irb>
    ?> width = my_data.keys.inject(0) {|wmax,dns| [dns.length,
    wmax].max }
    => 29
    irb> my_data.each do |dns,ip|
    ?> puts "#{dns.ljust(width)} => #{ip}"
    irb> end
    www.google.com => 216.68.119.70
    www.ibm.com => 129.42.60.216
    www.letmegooglethatforyou.com => 209.20.88.2
    www.iowahawkeyes.com => 199.108.163.135
    => {"www.google.com"=>"216.68.119.70", "www.ibm.com"=>"129.42.60.216",
    "www.letmegooglethatforyou.com"=>"209.20.88.2",
    "www.iowahawkeyes.com"=>"199.108.163.135"}

    You could also put a limit on the width of the column so that one long
    value doesn't "win" or analyze the widths for some other property
    (like rounding up to a multiple of 4 or something).

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Dec 8, 2009
    #5
  6. 2009/12/8 Steve Wilhelm <>:
    > Thomas Sawyer wrote:
    >> On Dec 7, 11:26=EF=BF=BDpm, Jon Kim <> wrote:
    >>> www.google.com=EF=BF=BD =EF=BF=BD =3D> 123.123.123.12www.ibm.com=EF=BF=

    =BD =EF=BF=BD =EF=BF=BD =EF=BF=BD=3D> 123.112.123.12www.iowahawkeyes=EF=BF=
    =BD =3D> 123.123.112.12
    >>>
    >>> original code was
    >>>
    >>> puts (dns + " =3D> " + ip)
    >>>
    >>> I tried next code
    >>>
    >>> puts (dns + " =EF=BF=BD"*(20 - dns.length) + " =3D > " + ip)
    >>>
    >>> but it did not work well. please teach me how to do it.

    >>
    >> "%-30s %s" % [dns, ip]

    >
    > Thomas's suggestion of String Format (%) is correct, just revising
    > example to reflect the original code above.
    >
    > puts "%-20s =3D> %s" % [dns, ip]


    I'd prefer printf in that case

    printf "%-20s =3D> %s\n", dns, ip

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Dec 8, 2009
    #6
  7. Jon Kim

    W. James Guest

    Jon Kim wrote:

    > Hi,
    >
    > I am newbie in Ruby. I have a question about indenting string.
    >
    > there is strings like below:
    >
    > www.google.com => 123.123.123.12
    > www.ibm.com => 123.112.123.12
    > www.iowahawkeyes => 123.123.112.12
    >
    > i want to make above like next
    >
    > www.google.com => 123.123.123.12
    > www.ibm.com => 123.112.123.12
    > www.iowahawkeyes => 123.123.112.12
    >
    >
    > original code was
    >
    > puts (dns + " => " + ip)
    >
    > I tried next code
    >
    > puts (dns + " "*(20 - dns.length) + " = > " + ip)
    >
    > but it did not work well. please teach me how to do it.


    data =
    %w(blip bleeping rob_the_fraud tor what-pompous-verbosity dreadful).
    map{|s| [s + '.com', (1..4).map{rand 256}.join('.')] }

    width = data.map{|a,b| a.size}.max

    puts data.map{|a,b| [a.ljust(width),b].join " => " }


    === output ===

    blip.com => 249.7.84.219
    bleeping.com => 14.120.94.124
    rob_the_fraud.com => 252.63.171.160
    tor.com => 173.239.40.90
    what-pompous-verbosity.com => 56.61.184.48
    dreadful.com => 31.56.145.209


    --
    W. James, Dec 9, 2009
    #7
  8. On Dec 9, 2009, at 3:55 AM, W. James wrote:
    > Jon Kim wrote:
    >> Hi,
    >>
    >> I am newbie in Ruby. I have a question about indenting string.
    >>
    >> there is strings like below:
    >>
    >> www.google.com => 123.123.123.12
    >> www.ibm.com => 123.112.123.12
    >> www.iowahawkeyes => 123.123.112.12
    >>
    >> i want to make above like next
    >>
    >> www.google.com => 123.123.123.12
    >> www.ibm.com => 123.112.123.12
    >> www.iowahawkeyes => 123.123.112.12
    >>
    >>
    >> original code was
    >>
    >> puts (dns + " => " + ip)
    >>
    >> I tried next code
    >>
    >> puts (dns + " "*(20 - dns.length) + " = > " + ip)
    >>
    >> but it did not work well. please teach me how to do it.

    >
    > data =
    > %w(blip bleeping rob_the_fraud tor what-pompous-verbosity dreadful).
    > map{|s| [s + '.com', (1..4).map{rand 256}.join('.')] }
    >
    > width = data.map{|a,b| a.size}.max
    >
    > puts data.map{|a,b| [a.ljust(width),b].join " => " }
    >
    >
    > === output ===
    >
    > blip.com => 249.7.84.219
    > bleeping.com => 14.120.94.124
    > rob_the_fraud.com => 252.63.171.160
    > tor.com => 173.239.40.90
    > what-pompous-verbosity.com => 56.61.184.48
    > dreadful.com => 31.56.145.209



    So you missed the phrase "please teach me how to do it" from the OP,
    huh?

    Sorry that you're offended by my distinction between a request to
    "teach me" versus "show me".

    I hope Jon Kim <> gets something from all the
    responses.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Dec 9, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JSOUL
    Replies:
    3
    Views:
    459
    Barbara de Zoete
    Nov 9, 2005
  2. ennio
    Replies:
    1
    Views:
    320
    Jukka K. Korpela
    Sep 17, 2006
  3. dt
    Replies:
    4
    Views:
    468
    CBFalconer
    Dec 31, 2006
  4. JMG
    Replies:
    2
    Views:
    477
  5. why the lucky stiff
    Replies:
    5
    Views:
    134
    why the lucky stiff
    Sep 22, 2004
Loading...

Share This Page