Ternary operator request

R

Robert Mannl

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
 
A

Alex Fenton

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
 
D

David A. Black

Hi --

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
 
A

Ara.T.Howard

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

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
===============================================================================
 
R

Robert

I hadn't thought of that.

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


Rob
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top