# I think this is cool, ruby iterators and continuations

**URL:** <https://www.ruby-forum.com/t/i-think-this-is-cool-ruby-iterators-and-continuations/122126>\
**Category:** Ruby\
**Created:** [December 4, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/i-think-this-is-cool-ruby-iterators-and-continuations/122126 "2007-12-04T20:20:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 4, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/i-think-this-is-cool-ruby-iterators-and-continuations/122126/1 "2007-12-04T20:20:46Z")

</div>

=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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 11:30am UTC](https://www.ruby-forum.com/t/i-think-this-is-cool-ruby-iterators-and-continuations/122126/2 "2007-12-05T11:30:56Z")

</div>

On Dec 4, 1:18 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> statement.each_with_index do |stat,idx|
> 
> ```
> 
> 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
