ternary operator confusion

B

Belorion

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 =3D nil
true ? a =3D 1 : a =3D 2=20
-> a =3D 1=20

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

Why the syntax error?

-Matt
 
E

Eric Mahurin

--- "Marcel Molina Jr. said:
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?






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

ES

Le 1/6/2005 said:
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
 
A

Austin Ziegler

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).
=20
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).
=20
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.

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

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
J

Jamis Buck

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.

- Jamis
 
C

Charles Hixson

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

E
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.)
 
E

Eric Mahurin

--- Jamis Buck said:
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.

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.




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

Gavin Kistner

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

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

Hal Fulton

Eric said:
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
 
E

Eric Mahurin

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

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






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

Robert Klemme

Marcel said:
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
 
L

Logan Capaldo

For comparision here ways that some languages make
function/method/procedure/command calls:
=20
function/method/procedure/command: f
arguments: a, b
=20
`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
=20

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

Caleb Clausen

Eric said:
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 said:
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.
 
E

Eric Mahurin

--- Caleb Clausen said:
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.
Works for me....

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





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

Yukihiro Matsumoto

Hi,

In message "Re: ternary operator confusion"

|respond_to?:test # syntax error, why?

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

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top