5 kyu

Custom Enumerators - Fibonacci

1,017 of 1,067pugio

Description:

Ruby has very powerful enumerator support, including the ability to create your own custom enumerators on the fly.

digits = Enumerator.new do |y|
  i = 0
  loop do
    y.yield i
    i += 1
  end
end

digits.next # => 0
digits.next # => 1

digits.take_while {|n| n < 13} # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 

Create an enumerator "fib" that outputs successive fibonacci numbers.

For example:

fib.next # => 1
fib.next # => 1
fib.next # => 2

fib.take_while {|n| n < 100} # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] 

** NOTE: The first value returned from the enumerator will be 1, not 0**

Fundamentals

Stats:

CreatedApr 12, 2013
PublishedApr 12, 2013
Warriors Trained1918
Total Skips886
Total Code Submissions4536
Total Times Completed1067
Ruby Completions1017
Total Stars21
% of votes with a positive feedback rating84% of 59
Total "Very Satisfied" Votes43
Total "Somewhat Satisfied" Votes13
Total "Not Satisfied" Votes3
Ad
Contributors
  • pugio Avatar
Ad