From: Bertram Scharpf on
Hi,

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
> precedence.

There's a difference between _grouping_ parentheses (like
begin...end) and _method_ _call_ parentheses:

puts (nil or 4) # vs.
puts( nil or 4)

See:

irb(main):001:0> $-w = true
=> true
irb(main):002:0> puts( nil or 4)
SyntaxError: compile error
(irb):2: syntax error, unexpected kOR, expecting ')'
puts( nil or 4)
^
from (irb):2
irb(main):003:0> puts (nil or 4)
(irb):3: warning: (...) interpreted as grouped expression
4
=> nil


Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Support `String#notempty?': <http://raa.ruby-lang.org/project/step>.

From: Seebs on
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)"?

Apparently not.

-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: Marnen Laibow-Koser on
Albert Schlef wrote:
> Marnen Laibow-Koser wrote:
>> Albert Schlef wrote:
>>> Albert Schlef wrote:
>>>
>>> Hey, I now see that this works:
>>>
>>> some_func((ARGV[0] 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?

begin

rescue

end

Look it up.

Or is that not what you didn't understand?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen(a)marnen.org
--
Posted via http://www.ruby-forum.com/.

From: Charles Oliver Nutter on
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
>> 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).

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 :)

- Charlie

From: Charles Oliver Nutter on
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[0] 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[0] || raise "You must provide an argument")
>
> It still didn't work. So finally I did:
>
>  some_func(ARGV[0] || (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[0] || raise('you must provide an argument')

or:

some_func(ARGV[0] || raise('you must provide an argument')

And if I were to write this, I'd probably choose one of these variants:

ARGV[0] ? some_func(ARGV[0]) : raise 'you must provide an argument'

or

raise 'you must provide an argument' unless ARGV[0]
some_func(ARGV[0])

- Charlie