Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > counter

Reply
Thread Tools

counter

 
 
Lucas Handelsman
Guest
Posts: n/a
 
      11-02-2006
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/.

 
Reply With Quote
 
 
 
 
Gabriele Marrone
Guest
Posts: n/a
 
      11-02-2006

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.


 
Reply With Quote
 
 
 
 
Lutz Horn
Guest
Posts: n/a
 
      11-03-2006
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/.

 
Reply With Quote
 
Patrick Spence
Guest
Posts: n/a
 
      11-03-2006
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/.

 
Reply With Quote
 
Lucas Handelsman
Guest
Posts: n/a
 
      11-03-2006
Thanks guys, I will give it a shot and let you know how it goes.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Patrick Spence
Guest
Posts: n/a
 
      11-03-2006
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Reset network creation counter J.H. Holliday Wireless Networking 4 06-11-2005 08:09 PM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
Java Applet counter doesn't display correctly Dick Stouffer Firefox 8 10-05-2004 04:52 PM
can I do such a simplest counter in VHDL? walala VHDL 3 09-14-2003 08:05 PM



Advertisments