Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ternary operator request

1 view
Skip to first unread message

Robert Mannl

unread,
Sep 14, 2005, 11:22:41 AM9/14/05
to
Hi!

I'm more or less an amateur programmer, but I have fallen in love
with Ruby.

I have an improvement idea for Ruby - I don't know if this is the
best place to post this, but I thought it might spark some discussion
about whether adding this "feature" would be healthy for the language
or not.

Let's suppose we have a method called: "some_method?". Why not allow
writing ternary operator expressions like this:

some_method? a : b

instead of

some_method? ? a : b


It feels way more natural,

Rob


Alex Fenton

unread,
Sep 14, 2005, 11:43:26 AM9/14/05
to
That's a cute idea. I do find myself writing code like

something.zero? ? x : y

But presently there's nothing "special" about method names that end in
"?" -
it's only conventional that they return a boolean, and they don't
necessarily
have no arguments. In your example

some_method? a : b

how does ruby and the reader distinguish whether 'a' is

1) the first option for the ternary operator's return value or
2) the first argument to some_method?

As an example, this syntax doesn't look quite so appealing...

an_array.include? val x : y

alex

David A. Black

unread,
Sep 14, 2005, 12:10:38 PM9/14/05
to
Hi --

On Thu, 15 Sep 2005, Robert Mannl wrote:

> Hi!
>
> I'm more or less an amateur programmer, but I have fallen in love with Ruby.

Welcome!

> I have an improvement idea for Ruby - I don't know if this is the best place
> to post this, but I thought it might spark some discussion about whether
> adding this "feature" would be healthy for the language or not.
>
> Let's suppose we have a method called: "some_method?". Why not allow writing
> ternary operator expressions like this:
>
> some_method? a : b
>
> instead of
>
> some_method? ? a : b
>
>
>
>
> It feels way more natural,

I imagine it would be a nightmare to parse, for Ruby and for the human
eye.

if x? 1 : 2; end

Is that:

if x?(1) then 2; end

or

if x? ? true : true; end

etc. (Not great examples, I know, but they indicate how vexed the ?
and : could get.)

I also think the two ?'s in question, though both ?'s, are really
semantically quite distinct.

There's always 'if' :-)


David

--
David A. Black
dbl...@wobblini.net


Ara.T.Howard

unread,
Sep 14, 2005, 12:25:08 PM9/14/05
to
On Thu, 15 Sep 2005, Robert Mannl wrote:

though this only works with the attribute type methods provided by traits:

harp:~ > cat a.rb
require 'traits'

trait 'foo' => true

puts( foo ? 'foo' : 'not foo' )
puts( foo? ? 'foo' : 'not foo' )

foo false

puts( foo ? 'foo' : 'not foo' )
puts( foo? ? 'foo' : 'not foo' )


harp:~ > ruby a.rb
foo
foo
not foo
not foo

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================

Robert

unread,
Sep 14, 2005, 4:54:21 PM9/14/05
to
I hadn't thought of that.

I'll try to wrap my mind around this later, but you seem to be right :)


Rob


0 new messages