Loop, insdide do loop. do twice?

mply = 0
body = “just some data”
proof = 100
VALUE=""
list = [‘line1’,‘line2’,‘line3’]

foreach(list) do |block|
while mply < 3
puts mply.to_s+block+VALUE
if body.bytesize > proof
print(“writing to log.html #{VAULE}”)
end
mply = mply + 1
end
mply = 0
end
VALUE=“XXXX”

if you run this example above, you should see something printed like

this

0line1

1line2

2line3

now what im trying to do could easly be done with just doubling this
code but that is messy and i need to clean it up…

how can i produce this output and keep things short and simple

0line1

0line1XXXX

1line2

1line2XXXX

2line3

2line3XXXX

im in irc /server freenode #ruby if some one could help me or just
respond here… this one has me scratching my head for a few days…

On Thu, Feb 3, 2011 at 7:57 AM, Bigmac T. [email protected]
wrote:

mply = 0
body = “just some data”
proof = 100
VALUE=""
list = [‘line1’,‘line2’,‘line3’]

foreach(list) do |block|
while mply < 3

You can use #times for iteration.

puts mply.to_s+block+VALUE
if body.bytesize > proof
print(“writing to log.html #{VAULE}”)

Note that print does not append a line terminator (as puts does).

end
mply = mply + 1
end
mply = 0
end
VALUE=“XXXX”

Reassigning a constant is a bad idea.

how can i produce this output and keep things short and simple

0line1

0line1XXXX

1line2

1line2XXXX

2line3

2line3XXXX

im in irc /server freenode #ruby if some one could help me or just
respond here… this one has me scratching my head for a few days…

This all can be solved with nested iterations - you just need to stack
your loops appropriately: the outer loop iterates what changes most
infrequently (“list” in your example). The next inner loop must
iterated over next infrequent changing item etc. Also, you can use
#each_with_index to get the index of each element in your list.

08:57:01 ~$ ruby19 x
0line1
0line1XXX
1line2
1line2XXX
2line3
2line3XXX
09:03:24 ~$ cat x
proof = 100
body = “just some data”

%w{line1 line2 line3}.each_with_index do |block, mypl|
[’’, ‘XXX’].each do |suffix|
puts “#{mypl}#{block}#{suffix}”
puts “Writing to log.html #{suffix}” if body.bytesize > proof
end
end
09:03:36 ~$

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs