|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
=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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Dec 4, 1:18 pm, "wbsurf...@yahoo.com" <wbsurf...@gmail.com> wrote:
> =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 |
|
![]() |
| Outils de la discussion | |
|
|