mply=0
kill=0
open(“list.lfi”,“rb”).each do |block|
[‘lol’, ‘%00’].each do |suffix|
while mply < 10
if block.include? “.log”
puts suffix
suffix
kill=1
break
print("!!! Check your log.html !!!")
end
if kill==1; break;end
mply=mply+1
end
if kill==1; break;end
mply=0
end
if kill==1; break;end
end
print suffix.last
i always do things the hard way… i dont know how to get suffix out
side of the loop…
mply=0
kill=0
open(“list.lfi”,“rb”).each do |block|
[‘lol’, ‘%00’].each do |suffix|
while mply < 10
if block.include? “.log”
puts suffix
suffix
kill=1
break
print("!!! Check your log.html !!!")
end
if kill==1; break;end
mply=mply+1
end
if kill==1; break;end
mply=0
end
if kill==1; break;end
end
print suffix.last
i always do things the hard way… i dont know how to get suffix out
side of the loop…
If a local variable is first assigned to inside of a block, then its
scope does not extend outside of the block (i.e. it’s local to that
block).
So you can assign to it outside the block:
foo = nil
10.times do |x|
foo = x
end
puts foo # remembers value
However, in this case ‘suffix’ is itself a block argument. In ruby 1.9,
it’s forced to be local to that block. In ruby 1.8 the block argument
can be a variable which has already been assigned to, but that’s bad
practice.
suffix = nil
[‘lol’, ‘%00’].each do |suffix
… do stuff
end
puts suffix # doesn’t work in ruby 1.9
So if you want the value of ‘suffix’ used in the last iteration of the
loop, simply remember it explicitly.
last_suffix = nil
[‘lol’, ‘%00’].each do |suffix|
last_suffix = suffix
… do stuff
end
puts last_suffix
mply=0
kill=0
open(“list.lfi”,“rb”).each do |block|
Another remark: you do not close the IO object properly that is opened
in the line above. Rather do
File.open(“list.lfi”,“rb”) do |io|
io.each do |block|
…
end
end
Btw, I’d consider “line” a better name for the block parameter here.
Although: you open the file with mode “rb” but use a line based
parsing scheme (by using IO#each) - that looks at least odd.
[‘lol’, ‘%00’].each do |suffix|
while mply < 10
Why do you use a while loop here? Doesn’t really look reasonable
since you process the same line (variable “block”) over and over
again.
if kill==1; break;end
side of the loop…
This really looks overly hard. Can you describe what that code is
supposed to do?
Cheers
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.