pass the paramater?

  • Thread starter Bigmac Turdsplash
  • Start date
B

Bigmac Turdsplash

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...
 
I

Iñaki Baz Castillo

2011/2/6 Bigmac Turdsplash said:
=C2=A0while mply < 10
=C2=A0 =C2=A0 =C2=A0if block.include? ".log"
=C2=A0 =C2=A0 =C2=A0 =C2=A0puts suffix
=C2=A0 =C2=A0 =C2=A0 =C2=A0suffix
=C2=A0 =C2=A0 =C2=A0 =C2=A0kill=3D1

I don't know what you expect from your given code (you didn't explain
it). However, the line "suffix" in the above code does nothing.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
B

Brian Candler

Bigmac Turdsplash wrote in post #979843:
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
 
R

Robert Klemme

Bigmac Turdsplash wrote in post #979843:

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|
=A0 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.

This really looks overly hard. Can you describe what that code is
supposed to do?

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top