The following Ruby code
下面的Ruby代码
def a(b,c) b+c end
is the same as follows with Python
Python是否与以下内容相同
def a(b,c): return b+c
It looks like that ruby has the special storage(stack or something) that stores the final evaluation result and returns the value when a function is called.
看起来ruby有特殊的存储(栈或其他东西),存储最终的评估结果,并在调用函数时返回值。
- If so, what's the name of the stack, and how can I get that stack?
- 如果是,堆栈的名称是什么,我如何获得堆栈?
- If not, how does the Ruby code work without returning something?
- 如果没有,Ruby代码如何工作而不返回一些东西?
4 个解决方案
#1
5
It's not that magic, Ruby just returns the value returned by the operation that does at the end.
它并不是那么神奇,Ruby只返回在结束时执行的操作返回的值。
It's synctactic sugar that it's implemented just at parsing level: a statement that calculates something implicitly returns itself without any keyword..
它只是在解析级别上实现的语法糖块:一个语句,它计算了一些不带任何关键字的隐式返回。
to clarify it a little bit you can imagine both abstract syntax trees of the two snippets: they won't be different.
稍微澄清一下,您可以想象这两个片段的两个抽象语法树:它们不会有什么不同。
#2
2
I don't think it's a stack. The final evaluation of the function is simply the return value, plain and simple. Just your everyday Ruby syntactic sugar.
我不认为这是堆栈。函数的最终值就是返回值,简单明了。你的日常红宝石语法糖。
#3
1
I don't see any reason why a stack should be required to return a result. A simple pointer to a memory location would be sufficient. I'd guess that would usually be returned in a register, such as EAX
.
我看不出为什么需要堆栈来返回结果。一个指向内存位置的简单指针就足够了。我猜这通常会在一个寄存器中返回,比如EAX。
You get the return value of a function by assigning the function's value to a variable (or doing something else with it). That's the way it was intended to be used, and the only way that works.
通过将函数的值赋给一个变量(或者用它来做别的事情)来获得函数的返回值。这就是它被使用的方式,也是唯一可行的方式。
Not returning anything is really easy: The called function doesn't put anything into the return location (whatever it may be) and the caller ignores it.
不返回任何东西真的很容易:被调用的函数不会将任何东西放入返回位置(不管它是什么),调用者会忽略它。
#4
1
Actually, return is special here, not the standard behavior. Consider:
实际上,这里返回是特殊的,而不是标准行为。考虑:
def foo(ary)
ary.each do |e|
return true if e == 2
end
end
This code actually has more then one stack frame (at least the on for #foo, the one for Array#each and the one for the anonymous function passed to #each). What return does: it does a jump to the stack frame of the outermost lexical scope it is called in (the end of foo) and returns the given value. If you play a lot with anonymous functions, you will find that return is no allowed in all context, while just returning the last computed value is.
这段代码实际上有不止一个堆栈帧(至少#foo是on,数组#是on,匿名函数传递给#each)。返回的作用是:它跳转到它在foo结尾调用的最外层词法范围的堆栈框架,并返回给定的值。如果您经常使用匿名函数,您会发现在所有上下文中都不允许返回,而只返回最后一个计算值。
So I would recommend never to use return if you don't need it for precisely that reason: breaking and returning from a running iteration.
因此,如果您不需要返回,我建议您永远不要使用return:从正在运行的迭代中中断和返回。
#1
5
It's not that magic, Ruby just returns the value returned by the operation that does at the end.
它并不是那么神奇,Ruby只返回在结束时执行的操作返回的值。
It's synctactic sugar that it's implemented just at parsing level: a statement that calculates something implicitly returns itself without any keyword..
它只是在解析级别上实现的语法糖块:一个语句,它计算了一些不带任何关键字的隐式返回。
to clarify it a little bit you can imagine both abstract syntax trees of the two snippets: they won't be different.
稍微澄清一下,您可以想象这两个片段的两个抽象语法树:它们不会有什么不同。
#2
2
I don't think it's a stack. The final evaluation of the function is simply the return value, plain and simple. Just your everyday Ruby syntactic sugar.
我不认为这是堆栈。函数的最终值就是返回值,简单明了。你的日常红宝石语法糖。
#3
1
I don't see any reason why a stack should be required to return a result. A simple pointer to a memory location would be sufficient. I'd guess that would usually be returned in a register, such as EAX
.
我看不出为什么需要堆栈来返回结果。一个指向内存位置的简单指针就足够了。我猜这通常会在一个寄存器中返回,比如EAX。
You get the return value of a function by assigning the function's value to a variable (or doing something else with it). That's the way it was intended to be used, and the only way that works.
通过将函数的值赋给一个变量(或者用它来做别的事情)来获得函数的返回值。这就是它被使用的方式,也是唯一可行的方式。
Not returning anything is really easy: The called function doesn't put anything into the return location (whatever it may be) and the caller ignores it.
不返回任何东西真的很容易:被调用的函数不会将任何东西放入返回位置(不管它是什么),调用者会忽略它。
#4
1
Actually, return is special here, not the standard behavior. Consider:
实际上,这里返回是特殊的,而不是标准行为。考虑:
def foo(ary)
ary.each do |e|
return true if e == 2
end
end
This code actually has more then one stack frame (at least the on for #foo, the one for Array#each and the one for the anonymous function passed to #each). What return does: it does a jump to the stack frame of the outermost lexical scope it is called in (the end of foo) and returns the given value. If you play a lot with anonymous functions, you will find that return is no allowed in all context, while just returning the last computed value is.
这段代码实际上有不止一个堆栈帧(至少#foo是on,数组#是on,匿名函数传递给#each)。返回的作用是:它跳转到它在foo结尾调用的最外层词法范围的堆栈框架,并返回给定的值。如果您经常使用匿名函数,您会发现在所有上下文中都不允许返回,而只返回最后一个计算值。
So I would recommend never to use return if you don't need it for precisely that reason: breaking and returning from a running iteration.
因此,如果您不需要返回,我建议您永远不要使用return:从正在运行的迭代中中断和返回。