ArgumentError in #new, Subclassing Enumerator

孤街浪徒 提交于 2019-12-07 13:59:30

问题


I'm subclassing Enumerator like this:

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

However...

> 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>'

Thoughts?


回答1:


What about:

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


来源:https://stackoverflow.com/questions/16016995/argumenterror-in-new-subclassing-enumerator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!