On Wed, Aug 30, 2023 at 10:43 AM konsolebox <konsolebox(a)gmail.com> wrote:
On Wed, Aug 30, 2023 at 2:26 PM Xavier Noria <fxn(a)hashref.com> wrote:
Yes, when () is meant to yield a expression, you can put several
statements in it.
For example, consider:
(x = Module.new; p x)::C = 1
It does work as observed and intuitively it should but the
documentation does not explicitly say it allows it to.
Yes. Unfortunately, the Ruby documentation is far from being comprehensive
or precise.
(a, b) does not play the role of an expression, right?
Similarly, in your
examples with puts,
puts(...)
is parsed as puts + the argument list of the call. Again, not an
expression. In an
argument list, a semicolon does not make sense, it is a
syntax error.
My example had a space between puts and '('. The documentation says:
"If you put a space between the method name and opening parenthesis,
you do not need two sets of parentheses." but 'puts (:ignore; :x)'
raises an error.
Oh, the space, right.
p (1 if true), (2 if false)
passes two arguments to p just fine.
The specs for this are here
<https://github.com/ruby/ruby/blob/00fdb4e12e1933bdb110aeecc08099b4875c91ce/spec/ruby/language/method_spec.rb#L1178>
(an
authoritative source when documentation is lacking), and I cannot give you
an answer to what you found without guessing. It could even be a bug in the
parser, because I don't quite see why is a ternary operator parsed, but not
an expression with ;.
I don't know.