From: Albert Schlef on 30 Dec 2009 21:24 (The problem is in both Ruby 1.8 and Ruby 1.9) The expression: puts(nil || 4) works as expected. It prints '4'. But this expression: puts(nil or 4) fails... it is a syntax error, for some mysterious reason. Why? I thought the only diference between "or" and "||" is the precedence. -- Posted via http://www.ruby-forum.com/.
From: Phillip Gawlowski on 30 Dec 2009 21:35 On 31.12.2009 03:24, Albert Schlef wrote: > (The problem is in both Ruby 1.8 and Ruby 1.9) > > The expression: > > puts(nil || 4) > > works as expected. It prints '4'. > > But this expression: > > puts(nil or 4) > > fails... it is a syntax error, for some mysterious reason. > > Why? I thought the only diference between "or" and "||" is the > precedence. Hm, on Ruby 1.9.1 I get: irb(main):001:0> puts nil or 4 => 4 irb(main):002:0> puts(nil or 4) SyntaxError: (irb):2: syntax error, unexpected keyword_or, expecting ')' puts(nil or 4) ^ from C:/Ruby19/bin/irb:12:in `<main>' irb(main):003:0> puts(nil || 4) 4 => nil This seems like a bug to me, since parenthesis *should* "only" make method calls and precedent unambigious. -- Phillip Gawlowski
From: Jörg W Mittag on 30 Dec 2009 21:38 Albert Schlef wrote: > [...] > puts(nil or 4) > > fails... it is a syntax error, for some mysterious reason. > > Why? I thought the only diference between "or" and "||" is the > precedence. Looks like a genuine bug to me. I can verify that behavior in MRI, YARV, JRuby, IronRuby and Rubinius (which is not terribly surprising since they all use the exact same parser). I'd be interested in XRuby, since that's the only sort-of complete (well, for a very generous definition of "complete", anyway) Ruby implementation I know of which has its own parser. (Tinyrb has its own parser, but it doesn't have full syntax compatibility as a goal anyway. Cardinal also has its own parser, AFAIK, but it's a *loong* way from being syntax-complete.) jwm
From: Seebs on 30 Dec 2009 21:43 On 2009-12-31, Phillip Gawlowski <pg(a)thimian.com> wrote: > Hm, on Ruby 1.9.1 I get: > irb(main):001:0> puts nil or 4 > >=> 4 Note that this is equivalent to "puts nil" followed by "4". That's significant. Consider: irb(main):001:0> x = nil or 4 => 4 irb(main):002:0> x => nil The grouping is: (x = nil) or (4) Or (puts nil) or (4) > This seems like a bug to me, since parenthesis *should* "only" make > method calls and precedent unambigious. I believe that's precisely the problem -- the precedence of "or" is low enough that it can't occur inside a method argument. Compare this with the similar situation in C, where a comma operator cannot occur inside an argument list for a function, because it's part of the function-call syntax. So: valid C: x = 1, y = 2; (this performs both assignments, and returns the value of y after the assignment, which happens to be 2.) printf("%d\n", x = 1, y = 2); This probably prints 1, but I think it's undefined behavior because there's excess arguments to printf. (I'm not sure whether that's permitted or not, but my guess would be "no, but it probably always works".) So try: irb(main):003:0> puts( (nil or 4) ) 4 => nil Basically, if you really want parentheses (as in the precedence-changing operator), you need to include them... Not merely put something that can't go in a method argument list inside the confusingly-similar-looking () which surround method arguments. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: David Masover on 30 Dec 2009 22:12
On Wednesday 30 December 2009 08:35:43 pm Phillip Gawlowski wrote: > On 31.12.2009 03:24, Albert Schlef wrote: > > (The problem is in both Ruby 1.8 and Ruby 1.9) > > > > The expression: > > > > puts(nil || 4) > > > > works as expected. It prints '4'. > > > > But this expression: > > > > puts(nil or 4) > > > > fails... it is a syntax error, for some mysterious reason. > > > > Why? I thought the only diference between "or" and "||" is the > > precedence. > > Hm, on Ruby 1.9.1 I get: > irb(main):001:0> puts nil or 4 > > => 4 > irb(main):002:0> puts(nil or 4) > SyntaxError: (irb):2: syntax error, unexpected keyword_or, expecting ')' > puts(nil or 4) > ^ > from C:/Ruby19/bin/irb:12:in `<main>' > irb(main):003:0> puts(nil || 4) > 4 > => nil Specifically, the parens around the method call. Want to see something even weirder? irb(main):001:0> puts(nil or) SyntaxError: (irb):1: syntax error, unexpected keyword_or, expecting ')' puts(nil or) ^ from /home/ruby19/bin/irb:12:in `<main>' irb(main):002:0> puts(nil or 4) SyntaxError: (irb):2: syntax error, unexpected keyword_or, expecting ')' puts(nil or 4) ^ from /home/ruby19/bin/irb:12:in `<main>' irb(main):003:0> (nil or 4) => 4 irb(main):004:0> puts((nil or 4)) 4 => nil irb(main):005:0> |