From: Albert Schlef on
Bertram Scharpf wrote:
[...]
> See:
>
> irb(main):001:0> $-w = true
[...]
> irb(main):003:0> puts (nil or 4)
> (irb):3: warning: (...) interpreted as grouped expression
> 4

Thanks, I didn't know of this $-w trick.
--
Posted via http://www.ruby-forum.com/.

From: Albert Schlef on
Phillip Gawlowski wrote:
> begin
> raise ArgumentError unless ARGV[0]
> puts ARGV[0]
> rescue ArgumentError
> puts "we had an argument error"
> end
>
> Basically, the begin/rescue/end tokens (bits that Ruby understands
> [...]

Thanks Phillip (and Charles Nutter) for all your help. In my 'raise' I
tried to mimic Perl's "or die" idiom. But I see that your example is
more elegant.


> > If "123 or true" isn't a statement, then it's a simple expression. And
> > then I would be able to do:
>
> Take a third option: It's an evaluation. ;)

For the time being I'll take the fourth option: The Ruby grammar is
weird in some corners, and we just have to accept it.
--
Posted via http://www.ruby-forum.com/.

From: Phillip Gawlowski on
On 01.01.2010 08:30, Albert Schlef wrote:

> Thanks Phillip (and Charles Nutter) for all your help. In my 'raise' I
> tried to mimic Perl's "or die" idiom. But I see that your example is
> more elegant.

Thank you. :)

However, my example is a plain by-the-books example of how Ruby does
exceptions.

So, thank Matz for creating something elegant in the first place. :)

> For the time being I'll take the fourth option: The Ruby grammar is
> weird in some corners, and we just have to accept it.

It is indeed. The more complex your desires get, the weirder Ruby gets,
IME. Overall, though, it's pretty clean and simple to use, and keeps
weirdness to the edge cases.

That's why I prefer a more verbose, explicit style of programming in
Ruby: It makes it clear to me what I attempt, and thus, hopefully,
Ruby's parser doesn't throw a wrench in my machinations. ;)

--
Phillip Gawlowski

From: David A. Black on
Hi --

On Thu, 31 Dec 2009, Phillip Gawlowski wrote:

> On 31.12.2009 11:35, Albert Schlef wrote:
>> Then why can I do this?
>>
>> puts (123 or true)
>>
>> Are you going to tell me that "123 or true", unlike "123 if true",
>> *isn't* a statement?
>
> I'm afraid I am. It's an evaluation. Boolean evaluation (with "and" / "or"
> having lower precedence than && / ||).
>
>> If "123 or true" isn't a statement, then it's a simple expression. And
>> then I would be able to do:
>
> Take a third option: It's an evaluation. ;)

Everything evaluates to something (an object) though. Evaluation is
more a process than a special kind of syntactic construct.

>> No, no, no. By `puts (123 if true)` I mean: output the result of `123 if
>> true`.
>
> That's what I said, I was just verbose about it. ;)

I think you said:

> It is obvious, to you, that you *mean* "output 123, if 123 is true".

The code doesn't test for the truth value of 123.


David

--
David A. Black
Senior Developer, Cyrus Innovation Inc.
THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
January 22-23, Tampa, Florida
Info and registration at http://www.thecompleatrubyist.com

From: David A. Black on
Hi --

On Thu, 31 Dec 2009, Phillip Gawlowski wrote:

> On 31.12.2009 06:32, Albert Schlef wrote:
>
>> It works. But, I must say, it isn't as beautiful as my original plan. It
>> doesn't read as English.
>
> PS C:\Scripts> ruby .\unless.rb
> ./unless.rb:1: Argument error (RuntimeError)
> PS C:\Scripts> ruby .\unless.rb "arg"
> PS C:\Scripts> cat .\unless.rb
> raise "Argument error" unless ARGV[0]
> PS C:\Scripts>
>
> I prefer it that way. :)
>
> Works in 1.8.6 and 1.9.1, too, to my great relief.

It even works in 1.0:

$ ruby10 -ve 'raise "Argument error" unless ARGV[0]'
ruby - version 1.0-971225 (i686-linux)
-e:1: RuntimeError: Argument error

I wouldn't worry about that idiom ceasing to work :-)


David

--
David A. Black
Senior Developer, Cyrus Innovation Inc.
THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
January 22-23, Tampa, Florida
Info and registration at http://www.thecompleatrubyist.com