Ternary operator confusion

I don't know if this is "improper" use of the ternary operator, but I
am curious as to why this is happening.

Simplified example:

a = nil
true ? a = 1 : a = 2
-> a = 1

a = []
true ? a.push 1 : a.push 2 (syntax error)

Why the syntax error?

-Matt

true ? a.push(1) : a.push(2)

"poetry mode" strikes again!

···

On 6/1/05, Phrogz <gavin@refinery.com> wrote:

true ? a.push(1) : a.push(2)

i suspect this could have been just a contrived example but you could also
do:

a.push true ? 1 : 2

marcel

···

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:

true ? a.push(1) : a.push(2)

--
Marcel Molina Jr. <marcel@vernix.org>

"poetry mode" strikes again!

> true ? a.push(1) : a.push(2)

ARG, now I feel dumb. Thanks for the heads up!

···

On 6/1/05, Phrogz <gavin@refinery.com> wrote:

I wish Ruby didn't make the () optional when passing args
(probably from Perl which wanted to be like shells/tcl/lisp
where command/function and arguments are space separated).

When you don't put the () in, the precendence can get confusing
and you arbitrarily prevent the first arg to start with a "("
(otherwise it will treat that "(" as what starts the argument
list).

Why do people feel the need to drop the ()? I didn't like it
in Perl and I don't like it in Ruby. Do people just want
something that is kind of like shell syntax?

···

--- "Marcel Molina Jr." <marcel@vernix.org> wrote:

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:
> true ? a.push(1) : a.push(2)

i suspect this could have been just a contrived example but
you could also
do:

a.push true ? 1 : 2

__________________________________
Discover Yahoo!
Get on-the-go sports scores, stock quotes, news and more. Check it out!
http://discover.yahoo.com/mobile.html

Marcel Molina Jr. wrote:

···

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:

true ? a.push(1) : a.push(2)

i suspect this could have been just a contrived example but you could
also do:

a.push true ? 1 : 2

In fact I'd say this is the preferred approach in this case. Also use

a = true ? 1 : 2

Both have less redundancy compared to the original code.

Kind regards

    robert

Less typing, uniform access, etc. Good news is that if one does
want to use parens, one is welcome to not do so.

E

···

Le 1/6/2005, "Eric Mahurin" <eric_mahurin@yahoo.com> a écrit:

--- "Marcel Molina Jr." <marcel@vernix.org> wrote:

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:
> true ? a.push(1) : a.push(2)

i suspect this could have been just a contrived example but
you could also
do:

a.push true ? 1 : 2

I wish Ruby didn't make the () optional when passing args
(probably from Perl which wanted to be like shells/tcl/lisp
where command/function and arguments are space separated).

When you don't put the () in, the precendence can get confusing
and you arbitrarily prevent the first arg to start with a "("
(otherwise it will treat that "(" as what starts the argument
list).

Why do people feel the need to drop the ()? I didn't like it
in Perl and I don't like it in Ruby. Do people just want
something that is kind of like shell syntax?

--
template<typename duck>
void quack(duck& d) { d.quack(); }

I use it in PDF::Writer because it makes certain parts feel more
like a DSL (domain specific language) than a program.

When I release this (later this week, I *hope*), it'll be visible in
the quickref code.

-austin

···

On 6/1/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:

--- "Marcel Molina Jr." <marcel@vernix.org> wrote:

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:

true ? a.push(1) : a.push(2)

i suspect this could have been just a contrived example but you
could also do:

a.push true ? 1 : 2

I wish Ruby didn't make the () optional when passing args
(probably from Perl which wanted to be like shells/tcl/lisp where
command/function and arguments are space separated).

When you don't put the () in, the precendence can get confusing
and you arbitrarily prevent the first arg to start with a "("
(otherwise it will treat that "(" as what starts the argument
list).

Why do people feel the need to drop the ()? I didn't like it in
Perl and I don't like it in Ruby. Do people just want something
that is kind of like shell syntax?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Eric Mahurin wrote:

I wish Ruby didn't make the () optional when passing args
(probably from Perl which wanted to be like shells/tcl/lisp
where command/function and arguments are space separated).

When you don't put the () in, the precendence can get confusing
and you arbitrarily prevent the first arg to start with a "("
(otherwise it will treat that "(" as what starts the argument
list).

Why do people feel the need to drop the ()? I didn't like it
in Perl and I don't like it in Ruby. Do people just want
something that is kind of like shell syntax?

Personally I like poetry mode in many cases (not all). When
it's unclear, I use parens (just as I do in math expressions).
Um, in case that didn't make sense: I sometimes use *unnecessary*
parens in expressions for clarity.

Does anyone *always* puts parens on method calls in Ruby?
Maybe you or others do. Would you say

    attr_reader(:foo)

for example? Or call the loop method this way?

    loop() { puts "I'm an infinite loop!" }

What about raise and throw? Or exit?

    raise(AnException)
    throw(:something)
    exit()

Be careful with super... these two can actually mean
different things:

     super
     super()

However, though I like poetry mode, I don't go as far as the
person who uses it in method definitions:

    def mymeth a,b,c # this always makes me blink

And I strongly tend to use parens in math functions:

    y = Math.sqrt(2)

Just my opinion.

Hal

And that is _precisely_ why I like the technique as well. It is wonderful for making a Ruby program read (more or less) like a domain document. And when ambiguity raises its ugly head, I can always throw in the parens to disambiguate.

But as someone else pointed out, if you don't like it, no one is forcing you to drop the parens in your own programs. Choose the technique that works best for you.

- Jamis

···

On Jun 1, 2005, at 3:47 PM, Austin Ziegler wrote:

On 6/1/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:

--- "Marcel Molina Jr." <marcel@vernix.org> wrote:

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:

true ? a.push(1) : a.push(2)

i suspect this could have been just a contrived example but you
could also do:

a.push true ? 1 : 2

I wish Ruby didn't make the () optional when passing args
(probably from Perl which wanted to be like shells/tcl/lisp where
command/function and arguments are space separated).

When you don't put the () in, the precendence can get confusing
and you arbitrarily prevent the first arg to start with a "("
(otherwise it will treat that "(" as what starts the argument
list).

Why do people feel the need to drop the ()? I didn't like it in
Perl and I don't like it in Ruby. Do people just want something
that is kind of like shell syntax?

I use it in PDF::Writer because it makes certain parts feel more
like a DSL (domain specific language) than a program.

ES wrote:

···

Le 1/6/2005, "Eric Mahurin" <eric_mahurin@yahoo.com> a écrit:

--- "Marcel Molina Jr." <marcel@vernix.org> wrote:

On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:
     

true ? a.push(1) : a.push(2)
       

i suspect this could have been just a contrived example but
you could also
do:

a.push true ? 1 : 2
     

I wish Ruby didn't make the () optional when passing args
(probably from Perl which wanted to be like shells/tcl/lisp
where command/function and arguments are space separated).

When you don't put the () in, the precendence can get confusing
and you arbitrarily prevent the first arg to start with a "("
(otherwise it will treat that "(" as what starts the argument
list).

Why do people feel the need to drop the ()? I didn't like it
in Perl and I don't like it in Ruby. Do people just want
something that is kind of like shell syntax?
   
Less typing, uniform access, etc. Good news is that if one does
want to use parens, one is welcome to not do so.

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }

Uniform access could have been achieved if only () was made optional. (A good choice in my mind.) OTOH, I'm not that happy with optional parenthesis where the parameters are required. To me that feels like a bad design choice. (And with some type faces the difference between (stuff) and {stuff} isn't all that obvious. But that's a font problem...mainly.)

When I first starting using Perl (v4) I thought it was neat
that I didn't need the parens, but then I got burned a few
times when I needed parens in the first arg. Then I realized
what an ugly hack at in the syntax this was. This was a bad
thing that was brought over from perl. Consider the following
method calls:

1. f a , b # OK
2. f a , (b) # OK
3. f (a)+0, b # *** now (a) looks like the args to f***
4. f 0+(a), b # putting in the 0+ prevented the problem above

The problem is if you start with #1, you may eventually need
for the the first arg to be some expression and you run into
#3.

For comparision here ways that some languages make
function/method/procedure/command calls:

function/method/procedure/command: f
arguments: a, b

`f a b` shells (bash also supports "$(f a b)")
[f a b] tcl
(f a b) lisp
f(a,b) most other languages including perl and ruby
f a,b alternate for perl and ruby

The problem is that perl/ruby support both of the last 2 forms.
This results in ambiguity if you are trying to use the last
form, but need argument "a" to start with a "(". The () form
wins the ambiguity in this case. The "f a,b" form could have
worked more cleanly if the "f(a,b)" form wasn't supported
(you'd have to group with "(f a,b)"). But since perl/ruby
supports both and the f(a,b) form wins ambiguities, the f a,b
form looks to work inconsistently. On top of that you have to
think about precedence more closely (what started this thread).

just my 2 cents.

···

--- Jamis Buck <jamis@37signals.com> wrote:

On Jun 1, 2005, at 3:47 PM, Austin Ziegler wrote:

> On 6/1/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
>
>> --- "Marcel Molina Jr." <marcel@vernix.org> wrote:
>>
>>> On Thu, Jun 02, 2005 at 01:40:23AM +0900, Phrogz wrote:
>>>
>>>> true ? a.push(1) : a.push(2)
>>>>
>>> i suspect this could have been just a contrived example
but you
>>> could also do:
>>>
>>> a.push true ? 1 : 2
>>>
>> I wish Ruby didn't make the () optional when passing args
>> (probably from Perl which wanted to be like
shells/tcl/lisp where
>> command/function and arguments are space separated).
>>
>> When you don't put the () in, the precendence can get
confusing
>> and you arbitrarily prevent the first arg to start with a
"("
>> (otherwise it will treat that "(" as what starts the
argument
>> list).
>>
>> Why do people feel the need to drop the ()? I didn't like
it in
>> Perl and I don't like it in Ruby. Do people just want
something
>> that is kind of like shell syntax?
>>
>
> I use it in PDF::Writer because it makes certain parts feel
more
> like a DSL (domain specific language) than a program.

And that is _precisely_ why I like the technique as well. It
is
wonderful for making a Ruby program read (more or less) like
a domain
document. And when ambiguity raises its ugly head, I can
always throw
in the parens to disambiguate.

But as someone else pointed out, if you don't like it, no one
is
forcing you to drop the parens in your own programs. Choose
the
technique that works best for you.

__________________________________
Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mailtour.html

Would you have prefered to be forced to write:
     my_object.foo=( the_value )
versus
     my_object.foo = the_value
?

···

On Jun 1, 2005, at 4:42 PM, Charles Hixson wrote:

Uniform access could have been achieved if only () was made optional. (A good choice in my mind.) OTOH, I'm not that happy with optional parenthesis where the parameters are required. To me that feels like a bad design choice. (And with some type faces the difference between (stuff) and {stuff} isn't all that obvious. But that's a font problem...mainly.)

Now you are bring operators into the picture. Many (as above)
do get translated to method calls, but operator syntax is
completely independent of normal method call syntax. Although
semantically the above is a method call, syntactically it is an
assignment. If syntactically is was a method call, you would
be able to do this:

my_object.foo=(a,b)

but you cannot because it treats (a,b) as the RHS which is
invalid. Also, consider that you can do the following which
get translated to 2 = methods.

my_object.foo,your_object.bar = a,b

Your argument is better suited for the ? methods that take an
argument (i.e. respond_to?). I have to admit that I don't
always put () around its argument because it it usually just a
Symbol and not an expression. But, these ? method calls do
seem to behave inconsistently:

respond_to? :test # valid
respond_to?:test # syntax error, why?
respond_to?(:test) # valid
self.respond_to? :test # valid
self.respond_to?:test # valid
self.respond_to?(:test) # valid

···

--- Gavin Kistner <gavin@refinery.com> wrote:

On Jun 1, 2005, at 4:42 PM, Charles Hixson wrote:
> Uniform access could have been achieved if only () was made

> optional. (A good choice in my mind.) OTOH, I'm not that
happy
> with optional parenthesis where the parameters are
required. To me
> that feels like a bad design choice. (And with some type
faces the
> difference between (stuff) and {stuff} Yisn't all that
obvious. But
> that's a font problem...mainly.)

Would you have prefered to be forced to write:
     my_object.foo=( the_value )
versus
     my_object.foo = the_value
?

__________________________________
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/weekend.html

Just to add to this list:
f a b languages with ML derived syntaxes (eg SML, Haskell, etc.)
a b f Forth, Postscript
[obj message: arg1 withSomeArg: arg2]; Objective-C (Smalltalk as well
if you drop the I think)

I can't think of any other syntaxes for this.

···

On 6/1/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:

For comparision here ways that some languages make
function/method/procedure/command calls:

function/method/procedure/command: f
arguments: a, b

`f a b` shells (bash also supports "$(f a b)")
[f a b] tcl
(f a b) lisp
f(a,b) most other languages including perl and ruby
f a,b alternate for perl and ruby

Eric Mahurin wrote:

f (a)+0, b # *** now (a) looks like the args to f***

This gives a warning, at least.

respond_to?:test # syntax error, why?

Works for me....

Hal Fulton wrote:

  def mymeth a,b,c # this always makes me blink

I first discovered this is possible when RubyLexer tripped over a
specimen of code that was using it. Little mysteries like this were
part of what made RubyLexer such an, ah, joy to write. (Yeah, that's
one word for it.)

Now I use this sometimes myself, mostly to create more test cases for
RubyLexer, since def headers in particular are tricky. It can be
convenient when playing around in irb.

Hi,

···

In message "Re: ternary operator confusion" on Thu, 2 Jun 2005 11:59:46 +0900, Eric Mahurin <eric_mahurin@yahoo.com> writes:

respond_to?:test # syntax error, why?

It's a bug in CVS HEAD (1.9). Will be fixed soon.

              matz.

Eric Mahurin wrote:
> f (a)+0, b # *** now (a) looks like the args to f ***

This gives a warning, at least.

I wouldn't mind if it was required to have no space between the
"f" and the "(" in the f(a,b) form. If that was the case, then
the above could be interpreted as the "f a,b" form. That would
remove the ambiguity and make the the "f a,b" syntactically
cleaner in my opinion.

> respond_to?:test # syntax error, why?

Works for me....

I was using a 1.9CVS version to test this. I guess it is a
bug.

···

--- Caleb Clausen <vikkous@gmail.com> wrote:

__________________________________
Discover Yahoo!
Get on-the-go sports scores, stock quotes, news and more. Check it out!
http://discover.yahoo.com/mobile.html