I wrote this simple class
class FibSequence
include Enumerable
def initialize(num)
@sequence = fib(num)
end
def fib(n)
vals = [1, 1]
return [1] if n == 1
return vals if n == 2
(n-2).times do
vals.push(vals[-1] + vals[-2])
end
return vals
end
def each(&block)
@sequence.each(&block)
end
end
when I call it like this:
f = FibSequence.new(6)
f.reject { |s| s.odd? }
f.each { |s| print(s,’:’) }
I expect => [2, 8]
but I get => 1:1:2:3:5:8:
Ruby’s
rejectmethod does return the result as new array. You thus have to write.