ãããä½ãæ°æ©è»¸ãããããã§ã¯ããã¾ããã def fib_gen() a, b = 1, 1 while yield(a) a, b = b, a + b end end fib_gen() do |x| print x, " " x < 5000 end #=> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765åã«ç·´ç¿ãã¦ããã ãã§ãã class Fib def initialize @a, @b = 0, 1 end def next @a, @b = @b, @a + @b @a end end fib = Fib.new 20.times { print fib.next(), " " } #=> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 15
{{#tags}}- {{label}}
{{/tags}}