From: Bertram Scharpf on 31 Dec 2009 09:15
Am Donnerstag, 31. Dez 2009, 11:24:26 +0900 schrieb Albert Schlef:
> (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
There's a difference between _grouping_ parentheses (like
begin...end) and _method_ _call_ parentheses:
puts (nil or 4) # vs.
puts( nil or 4)
irb(main):001:0> $-w = true
irb(main):002:0> puts( nil or 4)
SyntaxError: compile error
(irb):2: syntax error, unexpected kOR, expecting ')'
puts( nil or 4)
irb(main):003:0> puts (nil or 4)
(irb):3: warning: (...) interpreted as grouped expression
Support `String#notempty?': <http://raa.ruby-lang.org/project/step>.
From: Seebs on 31 Dec 2009 11:35
On 2009-12-31, Albert Schlef <albertschlef(a)gmail.com> wrote:
> so doens't this mean that wherever I can put "999" I can also put "(123
> if true)"?
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: Marnen Laibow-Koser on 31 Dec 2009 12:25
Albert Schlef wrote:
> Marnen Laibow-Koser wrote:
>> Albert Schlef wrote:
>>> Albert Schlef wrote:
>>> Hey, I now see that this works:
>>> some_func((ARGV or raise "You must provide an argument"))
>>> Great. On the other hand, I won't be able to remember this the next time
>>> I need it.
>> Just use Ruby's rescue syntax instead of trying to be clever.
> What do you mean?
Look it up.
Or is that not what you didn't understand?
Posted via http://www.ruby-forum.com/.
From: Charles Oliver Nutter on 31 Dec 2009 19:40
2009/12/30 JÃ¶rg W Mittag <JoergWMittag+Ruby(a)googlemail.com>:
> 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
> 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).
We do not all use the same parser. At the very least, they're all
written in our local language. We use parsers that were originally
ports of the original parser, and while changing over the years still
match MRI's parser behavior.
If MRI and YARV do it, then we do it because that's what MRI and YARV do.
Minor nitpick :)
From: Charles Oliver Nutter on 31 Dec 2009 19:47
On Wed, Dec 30, 2009 at 11:32 PM, Albert Schlef <albertschlef(a)gmail.com> wrote:
> Well. it turns out there aren't that many ways in ruby.
> I originally tried to do the following:
> Â some_func(ARGV or raise "You must provide an argument")
First off, I'd say that this is probably confusing form to use. If I
ever saw this in code I'd write it differently.
> I wish it worked. But it doesn't. So I changed it to:
> Â some_func(ARGV || raise "You must provide an argument")
> It still didn't work. So finally I did:
> Â some_func(ARGV || (raise "You must provide an argument"))
> It works. But, I must say, it isn't as beautiful as my original plan. It
> doesn't read as English.
This isn't too bad, for what you want:
some_func ARGV || raise('you must provide an argument')
some_func(ARGV || raise('you must provide an argument')
And if I were to write this, I'd probably choose one of these variants:
ARGV ? some_func(ARGV) : raise 'you must provide an argument'
raise 'you must provide an argument' unless ARGV