Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArgumentError in #new, Subclassing Enumerator

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?

like image 780
ichigolas Avatar asked Dec 06 '25 00:12

ichigolas


1 Answers

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
like image 138
Benjamin Tan Wei Hao Avatar answered Dec 08 '25 14:12

Benjamin Tan Wei Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!