<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>File: coroutine.rb</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



  <div id="fileHeader">
    <h1>coroutine.rb</h1>
    <table class="header-table">
    <tr class="top-aligned-row">
      <td><strong>Path:</strong></td>
      <td>lib/carat/coroutine.rb
      </td>
    </tr>
    <tr class="top-aligned-row">
      <td><strong>Last Update:</strong></td>
      <td>Tue Dec 07 02:08:23 EST 2004</td>
    </tr>
    </table>
  </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <h1><a href="../../../classes/Coroutine.html">Coroutine</a></h1>
<p>
Coroutines are program components like subroutines. Coroutines are more
generic and flexible than subroutines, but are less widely used in
practice. Coroutines were first introduced natively in Simula. Coroutines
are well suited for implementing more familiar program components such as
cooperative tasks, iterators, infinite lists, and pipes.
</p>
<h2>Example</h2>
<pre>
  count = (ARGV.shift || 1000).to_i
  input = (1..count).map { (rand * 10000).round.to_f / 100}
  Producer = Coroutine.new do |me|
    loop do
      1.upto(6) do
        me[:last_input] = input.shift
        me.resume(Printer)
      end
      input.shift # discard every seventh input number
    end
  end
  Printer = Coroutine.new do |me|
    loop do
      1.upto(8) do
        me.resume(Producer)
        if Producer[:last_input]
          print Producer[:last_input], &quot;\t&quot;
          Producer[:last_input] = nil
        end
        me.resume(Controller)
      end
      puts
    end
  end
  Controller = Coroutine.new do |me|
    until input.empty? do
      me.resume(Printer)
    end
  end
  Controller.run
  puts
</pre>
<h2>Author</h2>
<ul>
<li>Florian Frank

</li>
</ul>
<h2>History</h2>
<pre>
  $Id: coroutine.rb,v 1.0.0 2004/12/01 transami Exp $
</pre>

    </div>


   </div>


  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>