From: james@... Date: 2002-04-14T09:31:13+09:00 Subject: statements & expressions( was RE: not vs !, and vs && ) > From: Guy N. Hurst [mailto:gnhurst@hurstlinks.com] > > james@rubyxml.com wrote: > > ... > > So, 'and' and '&&' are *not* synonyms that differ only in precedence? > > (Likewise for '!' and 'not' ) > > > > They are synonyms, but one forms a statement, and the other forms an > expression. The difference is that you cannot use a statement as an > argument inside another statement or expression. And it is solved by > adding a set of parentheses. This can be considered a precedence issue, > I suppose, as far as parsing goes. OK. I guess then that if two things are called synonyms, I expect that they are essentially interchangeable (if not identical). If they only differ in evaluation precedence, then it seems that this, in turn, effects whether something is a expression or a statement. It *almost* seems like 'not' can't lead off an expression, because it "wants" to bind to something on its left, which isn't there. Or 'not' is insufficiently bound to the item to its right. By and large I'm looking for whatever essential principles of Ruby that, once grasped, make using even the relatively arcane aspects of Ruby fairly transparent. For example, "Everything's an object." It seems it would be easier, then, to explain the few exceptions to the general rules, rather than presenting too many things as special cases. > > "In Ruby's syntax, statement is just a special case of a expression > which cannot appear as a argument (e.g. multiple assignment)." > > > "Because method call without parentheses is considered as statement, > which can not be part of expression unless grouped within parentheses." > Is it accurate to say that every expression is an object? Is every statement an object? > > In Ruby, 'everything has its value'. Both expressions and statements > are executed and have a value. > > You might think expressions have a value and statements don't, but > that is not necessarily how it works in Ruby: > > "If every expression must have value by definition, it's OK for me to > call non-value-returning expressions as statements, but must it?" > > > Some expressions, such as 'while', 'class', and 'def', don't return > any 'value' (see ). Now, matz said they do > have one, they just aren't accessible ;-) Is the set of void expressions still the same as it was when that message was posted? > > "In Ruby, everything has its value, even definitions (although > they doesn't have meaningful values)." > > He makes them return 'void value expression' arbitrarily: > > "Some expressions (e.g. while, break, etc.) are marked as void > expression by the parser. I can relax them very easily if they bother > you rather than helping you." > > "... if we can find proper return value for `def', I'll > change expressions `class', `module', and 'def' to return values. > Otherwise remain as they are." I recently read that thread. I getting a return value from a while loop (hey, why not?), and 1.6.5 kept giving me that void expression thing. But 1.7.2 doesn't seem to complain. It just doesn't return a value ;) > > By the way, you can still use them as part of an argument if in > addition to adding parentheses you also specify an explicit value: > > m = (def meth(x); x.new; end; nil) > o = meth( (class MyClass; end; MyClass) ) Interesting.. Thanks for helping clear things up. James > > > Guy N. Hurst >