counter

Discussion in 'Ruby' started by Lucas Handelsman, Nov 2, 2006.

  1. Pretty new to ruby (and programming but am getting better).

    I am importing data with DBI and I want display and counter as each row
    is imported. I can get the display to show:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    etc...

    but I don't want a linebreak after the counter is displayed. I want it
    just to keep refreshing itself, basically. Am I missing something with
    puts or print or p? Any ideas?

    --
    Posted via http://www.ruby-forum.com/.
     
    Lucas Handelsman, Nov 2, 2006
    #1
    1. Advertising

  2. Il giorno 02/nov/06, alle ore 01:21, Lucas Handelsman ha scritto:

    > but I don't want a linebreak after the counter is displayed. I
    > want it
    > just to keep refreshing itself, basically. Am I missing something
    > with
    > puts or print or p? Any ideas?


    This isn't language dependent, and there isn't a "standard" way which
    will work under every environment.

    Under Unix you can print the character \r to go back to the beginning
    of the line, but keep in mind that stdout (unlike stderr) is line
    buffered: by default, it prints its output just when a line break is
    found. If you want to force it to write to your console, you have to
    flush it manually.
    Try something like this:

    10.times do |i|
    print "\r#{i} "
    sleep 0.5
    $stdout.flush
    end
    print "\n"

    I don't think it would work under Windows. Does it?
    Anyway, even if it won't work on a specific console, the user will be
    able to understand its output anyway (especially because of the space
    I left at the end of the string), so it shouldn't be a big issue.
     
    Gabriele Marrone, Nov 2, 2006
    #2
    1. Advertising

  3. Lucas Handelsman

    Lutz Horn Guest

    Lucas Handelsman wrote:
    > but I don't want a linebreak after the counter is displayed.


    You can set the varibale $\ (the output record separator for the print
    and IO#write, default is nil) to to a value you like.

    irb(main):001:0> $\ = "_"
    => "_"
    irb(main):002:0> [1, 2, 3].each {|i| print i}
    1_2_3_=> [1, 2, 3]

    This won't solve the flush problem, though.

    --
    Posted via http://www.ruby-forum.com/.
     
    Lutz Horn, Nov 3, 2006
    #3
  4. Lucas Handelsman wrote:
    > Pretty new to ruby (and programming but am getting better).
    >
    > I am importing data with DBI and I want display and counter as each row
    > is imported. I can get the display to show:
    >
    > 1
    > 2
    > 3
    > 4
    > 5
    > 6
    > 7
    > 8
    > 9
    > 10
    > etc...
    >
    > but I don't want a linebreak after the counter is displayed. I want it
    > just to keep refreshing itself, basically. Am I missing something with
    > puts or print or p? Any ideas?


    1.upto(50) {|index|
    print("\b" * index.to_s().length << index.to_s())
    sleep(0.2)
    }

    --
    Posted via http://www.ruby-forum.com/.
     
    Patrick Spence, Nov 3, 2006
    #4
  5. Lucas Handelsman, Nov 3, 2006
    #5
  6. Lucas Handelsman wrote:
    > Thanks guys, I will give it a shot and let you know how it goes.


    Sorry about the lack of explanation for the code snippet in my previous
    reply...

    #-- simple counter from 1 to 50
    1.upto(50) {|index|

    #-- print the number of backspaces necessary to delete the number,
    #-- this is what causes the number to appear to refresh
    print("\b" * index.to_s().length << index.to_s())

    #-- pause 2/10's of a second before returning to top of loop
    sleep(0.2)
    }


    --
    Posted via http://www.ruby-forum.com/.
     
    Patrick Spence, Nov 3, 2006
    #6
    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. walala
    Replies:
    3
    Views:
    10,157
    VhdlCohen
    Sep 14, 2003
  2. Jan Kindt
    Replies:
    1
    Views:
    1,502
    Jim Lewis
    Sep 17, 2003
  3. dong seok huh

    [Q] : async event counter

    dong seok huh, Oct 1, 2003, in forum: VHDL
    Replies:
    6
    Views:
    761
    Renaud Pacalet
    Oct 6, 2003
  4. The Eeediot
    Replies:
    3
    Views:
    2,238
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  5. George2
    Replies:
    1
    Views:
    803
    Alf P. Steinbach
    Jan 31, 2008
Loading...

Share This Page