Ruby 如何';案例';语句在函数中工作?

Ruby 如何';案例';语句在函数中工作?,ruby,recursion,fibonacci,Ruby,Recursion,Fibonacci,我有以下ruby代码: def xyz(n) case n when 0 0 when 1 1 else xyz(n - 1) + xyz(n - 2) end end puts xyz(ARGF.gets.to_i) 我执行代码如下: Ruby$ echo "4" | ruby test.rb 我想知道答案如何变成3。我不明白: xyz(n - 1) + xyz(n - 2) 代码的一部分。请解释。这是一个递归函数,通过本身调用函数 以下是函

我有以下ruby代码:

def xyz(n)
  case n
  when 0
    0
  when 1
    1
  else
    xyz(n - 1) + xyz(n - 2)
  end
end

puts xyz(ARGF.gets.to_i)
我执行代码如下:

Ruby$ echo "4" | ruby test.rb
我想知道答案如何变成
3
。我不明白:

xyz(n - 1) + xyz(n - 2)

代码的一部分。请解释。

这是一个
递归函数,通过
本身调用函数

以下是函数执行的不同
步骤
和每个步骤的
输出

第1步:

def xyz(n)
    n = 4.
    case n
    when 0
        0
    when 1
        1
    else
        n = 4, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(3) + xyz(2) // no result
    end
end
def xyz(n)
    n = 3.
    case n
    when 0
        0
    when 1
        1
    else
        n = 3, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(2) + xyz(1) // recursion stops here as case 1 is 1 // so o/p is 1 for hjere
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1
    else
        n = 2, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(1) + xyz(0) // 1
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1 // 1
    else
        n = 1, I wont come here
        xyz(n - 1) + xyz(n - 2)
    end
end
无o/p,因为这两个条件都进行递归调用


第二步:

def xyz(n)
    n = 4.
    case n
    when 0
        0
    when 1
        1
    else
        n = 4, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(3) + xyz(2) // no result
    end
end
def xyz(n)
    n = 3.
    case n
    when 0
        0
    when 1
        1
    else
        n = 3, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(2) + xyz(1) // recursion stops here as case 1 is 1 // so o/p is 1 for hjere
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1
    else
        n = 2, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(1) + xyz(0) // 1
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1 // 1
    else
        n = 1, I wont come here
        xyz(n - 1) + xyz(n - 2)
    end
end
o/p为1,因为xyz(1)将执行到1。 当前O/p:1 总体O/p:1


第三步:

def xyz(n)
    n = 4.
    case n
    when 0
        0
    when 1
        1
    else
        n = 4, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(3) + xyz(2) // no result
    end
end
def xyz(n)
    n = 3.
    case n
    when 0
        0
    when 1
        1
    else
        n = 3, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(2) + xyz(1) // recursion stops here as case 1 is 1 // so o/p is 1 for hjere
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1
    else
        n = 2, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(1) + xyz(0) // 1
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1 // 1
    else
        n = 1, I wont come here
        xyz(n - 1) + xyz(n - 2)
    end
end
此处o/p再次为1,因为
xyz(1)
将执行到
1
, 当前O/p:1 总体O/p:1+1=>2


第4步:

def xyz(n)
    n = 4.
    case n
    when 0
        0
    when 1
        1
    else
        n = 4, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(3) + xyz(2) // no result
    end
end
def xyz(n)
    n = 3.
    case n
    when 0
        0
    when 1
        1
    else
        n = 3, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(2) + xyz(1) // recursion stops here as case 1 is 1 // so o/p is 1 for hjere
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1
    else
        n = 2, so it comes here and calls
        xyz(n - 1) + xyz(n - 2)
        xyz(1) + xyz(0) // 1
    end
end
def xyz(n)
    n = 2.
    case n
    when 0
        0
    when 1
        1 // 1
    else
        n = 1, I wont come here
        xyz(n - 1) + xyz(n - 2)
    end
end
此处o/p再次为1,因为
情况1
将执行到
1
, 当前O/p:1 总体O/p:1+1+1=>3



因此,最终输出为
3

,称为递归。它是指函数直接或间接调用自身以收集最终结果。在这里或stackexchangeIt的计算机科学频道上阅读更多关于它的信息。这意味着
xyz(4)
是通过
xyz(3)+xyz(2)
计算的。你的问题不清楚:在你的标题中,你问case语句(在Ruby中实际上不存在,Ruby只有case表达式,没有case语句)是如何在函数中工作的(露比没有,Ruby只有方法,块,lambdas和PROC,但是没有函数)。然后在正文中,你问一个特定的方法调用是如何工作的。你能解释一下你感兴趣的是什么吗?另外,很多Ruyistor会考虑一个case表达式(特别是这个表达式)。代码气味。相反,一个guard子句就足够了,这样
defxyz(n);除非n>1否则返回n;xyz(n-1)+xyz(n-2);end;
approve…可以很好地理解