[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Adam Steinfurth

12/19/2008 1:22:00 AM

[Note: parts of this message were removed to make it a legal post.]

This program is very simple. It is suppose to load in the text file, load
each character into an array, then print out the array one letter at a
time. It is supposed to wait 1 second each time it prints. This freezes
every time I run it. Any ideas?

Adam


class Slow_Array

def array_split
@f = File.open('Text_for_TextReader.txt','r') do |f|
f.gets.scan(/\w/)
end
end

def print_and_wait
@f.each {
|i|
print i
sleep 1
}
end

end

#-------------------------------------------------
# Main logic
TextReader = Slow_Array.new

# Bring file into program
TextReader.array_split

# Print each character, waiting one second in between each printing
TextReader.print_and_wait

--
Put your computer to work.
www.volunteerathome.com

3 Answers

Einar Magnús Boson

12/19/2008 1:41:00 AM

0


On 19.12.2008, at 01:21 , Adam Steinfurth wrote:

> This program is very simple. It is suppose to load in the text
> file, load
> each character into an array, then print out the array one letter at a
> time. It is supposed to wait 1 second each time it prints. This
> freezes
> every time I run it. Any ideas?
>
> Adam
>
>
> class Slow_Array
>
> def array_split
> @f = File.open('Text_for_TextReader.txt','r') do |f|
> f.gets.scan(/\w/)
> end
> end
>
> def print_and_wait
> @f.each {
> |i|
> print i
> sleep 1
> }
> end
>
> end
>
> #-------------------------------------------------
> # Main logic
> TextReader = Slow_Array.new
>
> # Bring file into program
> TextReader.array_split
>
> # Print each character, waiting one second in between each printing
> TextReader.print_and_wait
>
> --
> Put your computer to work.
> www.volunteerathome.com


It doesn't freeze, but the output is buffered so it appears that way.
try adding a flush:

print i
STDOUT.flush
sleep 1


einarmagnus




Michael Fellinger

12/19/2008 2:02:00 AM

0

On Fri, Dec 19, 2008 at 10:21 AM, Adam Steinfurth <steinfal@gmail.com> wrote:
> This program is very simple. It is suppose to load in the text file, load
> each character into an array, then print out the array one letter at a
> time. It is supposed to wait 1 second each time it prints. This freezes
> every time I run it. Any ideas?
>
> Adam
>
>
> class Slow_Array
>
> def array_split
> @f = File.open('Text_for_TextReader.txt','r') do |f|
> f.gets.scan(/\w/)
> end
> end
>
> def print_and_wait
> @f.each {
> |i|
> print i
> sleep 1
> }
> end
>
> end
>
> #-------------------------------------------------
> # Main logic
> TextReader = Slow_Array.new
>
> # Bring file into program
> TextReader.array_split
>
> # Print each character, waiting one second in between each printing
> TextReader.print_and_wait

You can either:

$stdout.sync = true

or:

class SlowArray
def slow_each
File.open('Text_for_TextReader.txt') do |file|
while line = file.gets
puts line
sleep 1
end
end
end

SlowArray.new.slow_each

^ manveru

Adam Steinfurth

12/19/2008 8:05:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

STDOUT.flush
Did exactly what I wanted and it was an easy fix. I would have never
figured that out. Thank you.

Adam

On Thu, Dec 18, 2008 at 9:02 PM, Michael Fellinger <m.fellinger@gmail.com>wrote:

> On Fri, Dec 19, 2008 at 10:21 AM, Adam Steinfurth <steinfal@gmail.com>
> wrote:
> > This program is very simple. It is suppose to load in the text file,
> load
> > each character into an array, then print out the array one letter at a
> > time. It is supposed to wait 1 second each time it prints. This freezes
> > every time I run it. Any ideas?
> >
> > Adam
> >
> >
> > class Slow_Array
> >
> > def array_split
> > @f = File.open('Text_for_TextReader.txt','r') do |f|
> > f.gets.scan(/\w/)
> > end
> > end
> >
> > def print_and_wait
> > @f.each {
> > |i|
> > print i
> > sleep 1
> > }
> > end
> >
> > end
> >
> > #-------------------------------------------------
> > # Main logic
> > TextReader = Slow_Array.new
> >
> > # Bring file into program
> > TextReader.array_split
> >
> > # Print each character, waiting one second in between each printing
> > TextReader.print_and_wait
>
> You can either:
>
> $stdout.sync = true
>
> or:
>
> class SlowArray
> def slow_each
> File.open('Text_for_TextReader.txt') do |file|
> while line = file.gets
> puts line
> sleep 1
> end
> end
> end
>
> SlowArray.new.slow_each
>
> ^ manveru
>
>


--
Put your computer to work.
www.volunteerathome.com