# Counter

**URL:** <https://www.ruby-forum.com/t/counter/74218>\
**Category:** Ruby\
**Created:** [November 2, 2006, 1:21am UTC](https://www.ruby-forum.com/t/counter/74218 "2006-11-02T01:21:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [November 2, 2006, 1:21am UTC](https://www.ruby-forum.com/t/counter/74218/1 "2006-11-02T01:21:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [November 3, 2006, 11:18am UTC](https://www.ruby-forum.com/t/counter/74218/2 "2006-11-03T11:18:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [November 3, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/counter/74218/3 "2006-11-03T20:17:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [November 3, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/counter/74218/4 "2006-11-03T20:21:33Z")

</div>

Thanks guys, I will give it a shot and let you know how it goes.

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [November 3, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/counter/74218/5 "2006-11-03T20:39:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/counter/74218/6 "2006-11-07T10:53:35Z")

</div>

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.
