Read and Write (update) problem
-
hello.
I have a Ruby code that I want to use to gather data but it is to slow;
does anybody know why>?ContinueReading=0
def reeed(ContinueReading)
k=[]
File.open("C:/TT/temp.txt", "r") do |f|
f.each_line do |e|
k<<e
end
end
ContinueReading=k[0].to_i
puts (k[0])
return ContinueReading
endwhile reeed(ContinueReading)==1
reeed(ContinueReading)
puts ('I am Reading the Source file')
sleep(3)
break if reeed(ContinueReading)==0
end -
by the way you should replace ContinueReading with continuereading
the code would be:
continuereading=0def reeed(continuereading)
k=[]
File.open("C:/GHConnection/temp.txt", "r") do |f|
f.each_line do |e|
k<<e
end
end
continuereading= k[0].to_iputs (k[0])
return continuereading
endwhile reeed(continuereading)==1
reeed(continuereading)
puts ('I am Reading the Source file')
sleep(3)
break if reeed(continuereading)==0
end -
A simple way to read text from a file is this:
txt = IO.read("C:/TT/temp.txt")
which return it all as a string.
To read individual lines use:
lines = IO.readlines("C:/TT/temp.txt")
You can then look at each line as needed...
Remember to strip off the trailing "\n" thus:
lines.each{|line| p line; line.chomp!; p line }
-
thanks for your quick reply TIG.
so the code would be sth like this:continuereading=0
def reeed(continuereading)
lines = IO.readlines("C:/GHConnection/temp.txt")
return (lines[0]).to_i
endwhile ((reeed (continuereading)))==1
puts ('I am Reading the Source file')
sleep(3)
endit works fine but again it is 2 slow; all the characters in my text-file do not add up to 100 yet after iteration 3 the solution is practically worthless.
-
NO use:
lines = IO.readlines("C;/GHConnection/temp.txt").map{|l| l.chomp } puts( "Number of lines; #{lines.size}" ) for line in lines puts(line) # do whatever else with each line here end
Yes, using the
sleep()
with a 3 second argument will be slow ! -
well, we can change the sleep time (or we can omit it) but still 3-times reading is the best I can get out of this.
-
We are telling you that you should read the file ONCE, in it's ENTIRETY, into an array of lines.
THEN read from the array of lines, when ever you need to.
-
the problem is that I need the update-data in each cycle and the provider (the one that send the text files) is the one that decides when the reading action should stop. similar to web browsers when we want to get notified when sth changes.
I am using the sleep to make the action less frequent in the hope of achieving more/better iterations.
does that make sense? is there a better solution? -
Video of the process
Youtube Video -
Issue 1:
When your code [somehow] updates the model's contents if needs to only affect the relevant instances.
How are you getting ALL instances ?
How are you identifying your 'linked' instance definitions ?
It should be straightforward to separate the two 'types'...Issue 2:
Can you not set up a timer to poll the linked data ?
So instead of the user clicking on a button and sending an update 'request', you automate it on a click/and stop it on a click...
mytimer=UI.start_timer(1, true){ do_'reqeust'_every_second__BUT_when_@stopped_do(UI.stop_timer(mytimer)) }
That way it runs all of the time and refreshes the linked data every second... -
special thanks to you TIG, the timer was genius idea .
-
Advertisement