From: Patrick Toomey Date: 2006-09-25T03:55:45+09:00 Subject: Re: Riddle me this (a question about expressions) On Sep 24, 2006, at 5:45 AM, Robert Klemme wrote: > Others have commented this already. Just another bit: once you > think about semantics of "return" for a moment it should be > immediately clear that it is not and cannot be an expression simply > because it never returns control flow but instead transfers it to > someplace else. The same holds true for "next". > > Interestingly enough "raise" does not provoke such a compile error > although I cannot see how it would ever return control flow locally: > >>> RUBY_VERSION > => "1.8.3" >>> def foo() x = raise "Test" end > => nil > >>> RUBY_VERSION > => "1.8.5" >>> def foo() x = raise "Test" end > => nil > > Kind regards > > robert > > I guess that is what surprises me. I would expect return to work exactly like raise. It just seems strange to me that I can have a perfectly good expression such as if (3 < 4) return 5 end and then have that fail when I do x = if (3 < 4) return 5 end Someone else gave the example of simplifying my question down to the equivalent of trying the following x = return 9 Why shouldn't this be valid? As shown above, raise seems to return nil, and return seems like it should return a similar value if there is no other reasonable return value. Just for my own curiosity, what implications would returning "nil" have? Anyway, thanks for all your help. I realize most of this is of academic value, but it really helps to see how Ruby works underneath. Patrick