On 07.06.2007 16:52, Peter Marsh wrote:
>> primes = Series.new(Starting_value,Some_block_to_define_serires)
> 
> Just wanted to be a bit clearer about this, the second argument is a 
> block which can generate nth term in a series. This would probally take 
> a while for primes, but if it were recursive then it would be easier...

IMHO for a series it would be more natural to let the block calculate 
a[n+1] from a[n] wouldn't it?  Of course, for Fibonacci this would only 
work if you allow for multiple arguments.

Something like

#!ruby
class Serial
   include Enumerable

   def initialize(*init, &f)
     @init = init
     @f = f
   end

   def each(&b)
     a = b.arity
     current = @init
     loop do
       b[*current[0 ... a]]
       current = Array(@f[*current])
     end
     self
   end
end

s1 = Serial.new 0 do |x| x+1 end
s1.each {|x| p x; break if x > 10}

puts

s2 = Serial.new 0,1 do |a,b| [b,a+b] end
s2.each {|x| p x; break if x > 40}


Kind regards

	robert