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
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
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
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
===============================================================================
I'll try to wrap my mind around this later, but you seem to be right :)
Rob