I saw this example about the fibonacci sequence then tested it in IRB. Was hoping someone could break it down for me and show me how it's using the same method inside the method that is being defined.
我看到这个关于斐波那契数列的例子然后在IRB中测试它。希望有人能帮我把它分解,并向我展示它是如何在被定义的方法中使用相同的方法的。
def fib(n)
return n if (0..1).include? n
fib(n-1) + fib(n-2) if n > 1
end
2 个解决方案
#1
6
When you define a method with def whatever(...)
you are doing two things: creating a method, and assigning it to the named method slot.
当您用def whatever(…)定义一个方法时,您要做的事情有两件:创建一个方法,并将其分配给命名的方法槽。
When you invoke a method in Ruby it will look that up in the local scope; inside the fib
method that includes the current object on which fib
is defined. So, it finds the current definition of fib
and invokes it.
当您在Ruby中调用一个方法时,它将在本地范围中查找;在fib方法内部,该方法包括定义fib的当前对象。因此,它找到fib的当前定义并调用它。
I mention the current part because if the fib
method redefined fib
on the current object inside itself the new definition would be used, not the old definition.
我提到当前部分是因为如果fib方法在当前对象内部重新定义fib,将使用新的定义,而不是旧定义。
That is to say: it will dynamically find the code associated with the name each time the name is invoked.
也就是说:它将在每次调用名称时动态地找到与名称关联的代码。
#2
3
It is called recursion... It is a pattern that comes up in programming at times... If you want to program it is a tool/process you should learn. Instead of regurgitating here what is already out there, just read what is on wikipedia... Which contains an explanation of the fibonacci sequence which is the defacto example of recursion.
它被称为递归……这是在编程中出现的一种模式……如果你想编程,你应该学习它。不要在这里反反复复已有的内容,只要阅读*上的内容就可以了。它包含斐波那契数列的一个解释这是递归的实际例子。
http://en.wikipedia.org/wiki/Recursion
http://en.wikipedia.org/wiki/Recursion
#1
6
When you define a method with def whatever(...)
you are doing two things: creating a method, and assigning it to the named method slot.
当您用def whatever(…)定义一个方法时,您要做的事情有两件:创建一个方法,并将其分配给命名的方法槽。
When you invoke a method in Ruby it will look that up in the local scope; inside the fib
method that includes the current object on which fib
is defined. So, it finds the current definition of fib
and invokes it.
当您在Ruby中调用一个方法时,它将在本地范围中查找;在fib方法内部,该方法包括定义fib的当前对象。因此,它找到fib的当前定义并调用它。
I mention the current part because if the fib
method redefined fib
on the current object inside itself the new definition would be used, not the old definition.
我提到当前部分是因为如果fib方法在当前对象内部重新定义fib,将使用新的定义,而不是旧定义。
That is to say: it will dynamically find the code associated with the name each time the name is invoked.
也就是说:它将在每次调用名称时动态地找到与名称关联的代码。
#2
3
It is called recursion... It is a pattern that comes up in programming at times... If you want to program it is a tool/process you should learn. Instead of regurgitating here what is already out there, just read what is on wikipedia... Which contains an explanation of the fibonacci sequence which is the defacto example of recursion.
它被称为递归……这是在编程中出现的一种模式……如果你想编程,你应该学习它。不要在这里反反复复已有的内容,只要阅读*上的内容就可以了。它包含斐波那契数列的一个解释这是递归的实际例子。
http://en.wikipedia.org/wiki/Recursion
http://en.wikipedia.org/wiki/Recursion