Top-level return allows an argument
puts "Here" return 42 # or :foo, or any value
ruby test.rb Here
Should it be a SyntaxError, as mentioned in https://bugs.ruby-lang.org/issues/4840#note-24 ?
It seems confusing to accept it silently, as one could expect the exact code to be affected by it (that should not be the case imho).
Discovered in https://github.com/ruby/spec/pull/530
Updated by duerst (Martin Dürst) over 1 year ago
Wouldn't this be equivalent to C's return statement in main()?
It is used to tell the outer process (usually a shell) about the success (0) or failure (anything else than 0) of the program.
In the average shell, you should be able to test it with e.g.
ruby test.rb && echo "Previous process was successful."
which would not print the
"Previous process was successful." text because the return value was something else than 0.
Updated by Eregon (Benoit Daloze) over 1 year ago
Currently the argument is ignored.
And I think it would make little sense in a file loaded by #require to affect the global exit status if it does a "return 1".
"exit 1" can be used for that, top-level return is to avoid loading anything further in the file when it's not needed in my understanding.