3

我是这样子Enumerator类化的:

class CuadraticPrimeGenerator < Enumerator
  def initialize(a=1,b=49)
    super do |y|
      x = 1
      loop do 
        y << x**2 + a*x + b
        x += 1
      end
    end
  end
end

然而...

> CuadraticPrimeGenerator.new(1,49)
0027.rb:41:in `initialize': 49 is not a symbol (TypeError)
    from 0027.rb:41:in `initialize'
    from 0027.rb:48:in `new'
    from 0027.rb:48:in `<main>'

想法?

4

1 回答 1

3

关于什么:

class CuadraticPrimeGenerator < Enumerator
  def initialize(a=1,b=49)
    super() do |y|
      count, x = 0, 1
      loop { y.yield(x**2 + a*x + b) }
    end
  end
end
于 2013-04-15T14:07:07.000 回答