Next | 1.1 Methods | Prev |
Ruby has no functions, methods defined on the top level belong to the Object class, they may be used effectivelly as global functions, since every object class inherits from Object.
def fibUpTo(limit) a = Array.new n1 = n2 = 1 while n1 <= limit a << n1 yield n1 if block_given? n1, n2 = n2, n1+n2 end return a end fibUpTo(30) {|n| print "#{n} "} puts "", fibUpTo(10).reverse.join(', ')
1 1 2 3 5 8 13 21 8, 5, 3, 2, 1, 1
Next | Ruby for Perl programmers | Prev |