# from Programming Ruby, Dave Thomas def fib_up_to(max) i,j = 1,1 while i < max yield i # passes i to the block i, j = j, i+j end end fib_up_to(ARGV[0].to_i){ # {}, block only appears next |t| print t, " "} # to method puts p [1,3,5,7,9].find{ |v| v*v >30} # find uses iterator class Array def find for i in 0...size value = self[i] return value if yield(value) end return nil end end p [1,3,5,7,9].find{ |v| v*v >30} p [1,3,5,7].inject(0){ # s=0, the updates with sum |s,e| s+e} # e runs through the array p [1,3,5,7].inject(5){ |s,e| s+e} # See Generator for # external iterator