Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby have a special storage for returning a value?

The following Ruby code

def a(b,c) b+c end 

is the same as follows with 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.

  • 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?
like image 827
prosseek Avatar asked Feb 16 '26 14:02

prosseek


2 Answers

It's not that magic, Ruby just returns the value returned by the operation that does at the end.

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.

like image 171
Jack Avatar answered Feb 18 '26 03:02

Jack


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.

like image 22
Matchu Avatar answered Feb 18 '26 03:02

Matchu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!