ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: 6ftdan@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71662] [Ruby trunk - Feature #11734] Improved ternary operator
Date: Tue, 24 Nov 2015 20:28:54 +0000	[thread overview]
Message-ID: <redmine.journal-55065.20151124202853.660ed19f751e818c@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11734.20151124143335@ruby-lang.org

Issue #11734 has been updated by Daniel P. Clark.


`&` is associated more with proc.  I think `_` would be closer to the kind of thing you're looking for.

~~~ruby
some_long_expression = :baz
_ ? _.to_s : 'foobar'
# => "baz"
~~~

Although I tried in-lining it with a semicolon and found that the `_` feature only works off of the previous' lines results. So the following won't work.

~~~ruby
:baz; _ ? _.to_s : 'foobar'
# => "foobar"
~~~

And this doesn't work

~~~ruby
x = 4
:fiz ? _.to_s : 'foobar'
# => "4"
~~~

So if you don't mind putting your ternary operation on the next line you can just use `_`

----------------------------------------
Feature #11734: Improved ternary operator
https://bugs.ruby-lang.org/issues/11734#change-55065

* Author: Yurko Bregey
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
In ternary operator it would be nice to be able to pass expression result from *condition*  into *value_if_true/value_if_false* in such way:

`some_long_expression ? &.to_s : 'foobar'`
where `&` refers to `some_long_expression`

Instead of doing:
`some_long_expression ? some_long_expression.to_s : 'foobar'`

or:
`result = some_very_very_long_expression
result ? result.to_s : 'foobar'`



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2015-11-24 19:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-11734.20151124143335@ruby-lang.org>
2015-11-24 14:33 ` [ruby-core:71650] [Ruby trunk - Feature #11734] [Open] Improved ternary operator bregey
2015-11-24 15:10 ` [ruby-core:71651] [Ruby trunk - Feature #11734] " hanmac
2015-11-24 20:28 ` 6ftdan [this message]
2015-11-25  5:10 ` [ruby-core:71677] " hanmac
2015-11-25  9:34 ` [ruby-core:71679] " nobu
2015-11-25 10:06 ` [ruby-core:71680] " bregey
2015-11-25 10:23 ` [ruby-core:71681] " bregey
2015-11-25 13:00 ` [ruby-core:71683] " hanmac
2015-12-07  7:52 ` [ruby-core:71892] [Ruby trunk - Feature #11734] [Closed] " ko1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-55065.20151124202853.660ed19f751e818c@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).