+ 5

Recursion in Ruby

Can anyone explain this code please? def fib(num) if num < 2 num else fib(num-1) + fib (num - 2) end end puts fib(4)

8th Mar 2019, 10:53 AM
Isaac Jesudamilare
Isaac Jesudamilare - avatar
4 Réponses
+ 7
Because you have marked 4 for "num" variable, if 4 is not less than 2, so, Ruby go to add the 2 methods (num - 1) and (num + 2).
8th Mar 2019, 11:05 AM
program
program - avatar
8th Mar 2019, 11:02 AM
program
program - avatar
+ 2
You ruby Program are compute the fibonacci number of the giv'n number. Please look at wikipedia for more informations
8th Mar 2019, 11:50 AM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar
0
I want an example of Ruby
6th Jun 2021, 12:01 AM
حسين شكر فتحي المولى
حسين شكر فتحي المولى - avatar