I think this is cool, ruby iterators and continuations

W

wbsurfver

=begin

This might be old hat to some, but I just figured out how to make an
iterator so that if I get some kind of failure, it will return the
element and try again on the same iteration. The code below is just a
simulation, but now that I have this example working, I know how to
code what I really want. This feature of ruby really impresses me
alot !


=end


#####################################

$fail = true

$cont = nil

def proc_tab
statement = ["first statement", "second statement",
"third statement"]

statement.each_with_index do |stat,idx|
if $cont
cont = $cont
$cont = nil
cont.call
end
# simulate a failure on index 1
callcc {|cont| $cont = cont } if idx == 1 and $fail
$fail = false if $cont
yield stat
end
end



["searchCIO","searchCRM"].each do |site|
(1..5).each do |art|
$fail = true
puts site + "->" + art.to_s
proc_tab do |stat|
puts "stat:" + stat
end
end
end
 
M

MonkeeSage

=begin

This might be old hat to some, but I just figured out how to make an
iterator so that if I get some kind of failure, it will return the
element and try again on the same iteration. The code below is just a
simulation, but now that I have this example working, I know how to
code what I really want. This feature of ruby really impresses me
alot !

=end

#####################################

$fail = true

$cont = nil

def proc_tab
statement = ["first statement", "second statement",
"third statement"]

statement.each_with_index do |stat,idx|
if $cont
cont = $cont
$cont = nil
cont.call
end
# simulate a failure on index 1
callcc {|cont| $cont = cont } if idx == 1 and $fail
$fail = false if $cont
yield stat
end
end

["searchCIO","searchCRM"].each do |site|
(1..5).each do |art|
$fail = true
puts site + "->" + art.to_s
proc_tab do |stat|
puts "stat:" + stat
end
end
end

I'm not sure how useful it is to signal failure in the block of
proc_tab (seems like that would be something to go in the method body
instead; i.e., check the value and fix it/skip it/whatever before you
yield it to the block). But in any case, I don't think you actually
need continuations for this...

$fail = false

class IterError < Exception; end

def proc_tab
statement = ["first statement",
"second statement",
"third statement"]
statement.each_index { | idx |
begin
# simulate a failure on index 1
yield statement[idx]
raise IterError if idx == 1 and $fail
rescue IterError
$fail = false
retry
end
}
end

["searchCIO", "searchCRM"].each { | site |
(1..5).each { | art |
$fail = true
puts site + "->" + art.to_s
proc_tab { | stat |
puts "stat:" + stat
}
}
}

Regards,
Jordan
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top