Is there a good way to emulate yield
in Ruby? I'm interested in writing similar 'infinite fib sequence' in Ruby.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace cs2 {
class Program {
static void Main(string[] args) {
var i=Fibs().TakeWhile(x=>x < 1000).Where(x=>x % 2==0).Sum();
}
static IEnumerable<long> Fibs() {
long a = 0, b = 1;
while (true) {
yield return b;
b += a;
a = b - a;
}
}
}
}
If it is possible, please give an example.