ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:106041] [Ruby master Feature#18332] a ? b
@ 2021-11-13 18:41 dorianmariefr
  2021-11-13 19:34 ` [ruby-core:106042] " zverok (Victor Shepelev)
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: dorianmariefr @ 2021-11-13 18:41 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been reported by dorianmariefr (Dorian Marié).

----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106042] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
@ 2021-11-13 19:34 ` zverok (Victor Shepelev)
  2021-11-13 19:42 ` [ruby-core:106043] " dorianmariefr
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: zverok (Victor Shepelev) @ 2021-11-13 19:34 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by zverok (Victor Shepelev).


```ruby
puts "#{'font-bold' if true}"
# => font-bold
```

----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-94631

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106043] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
  2021-11-13 19:34 ` [ruby-core:106042] " zverok (Victor Shepelev)
@ 2021-11-13 19:42 ` dorianmariefr
  2021-11-13 20:36 ` [ruby-core:106044] " zverok (Victor Shepelev)
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dorianmariefr @ 2021-11-13 19:42 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by dorianmariefr (Dorian Marié).


Thanks, I still think the `root? ? "font-bold"` reads better than `"font-bold" if root?` though

----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-94632

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106044] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
  2021-11-13 19:34 ` [ruby-core:106042] " zverok (Victor Shepelev)
  2021-11-13 19:42 ` [ruby-core:106043] " dorianmariefr
@ 2021-11-13 20:36 ` zverok (Victor Shepelev)
  2021-11-14  0:42 ` [ruby-core:106046] " sawa (Tsuyoshi Sawada)
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: zverok (Victor Shepelev) @ 2021-11-13 20:36 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by zverok (Victor Shepelev).


Well, obviously that's a matter of personal taste, but for a one-branch `if` the postfix form seems to read naturally ("this item will have class "font-bold" if it is root). 

So it might be that `cond ? result` reads better for you because you invented it starting to think from `cond ? result : else` (which, should we say, is quite esoteric on itself, we just got used to it)


----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-94633

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106046] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
                   ` (2 preceding siblings ...)
  2021-11-13 20:36 ` [ruby-core:106044] " zverok (Victor Shepelev)
@ 2021-11-14  0:42 ` sawa (Tsuyoshi Sawada)
  2021-11-16  0:49 ` [ruby-core:106074] " shan (Shannon Skipper)
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: sawa (Tsuyoshi Sawada) @ 2021-11-14  0:42 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by sawa (Tsuyoshi Sawada).


`"font-bold" if root?` reads much better than `root? ? "font-bold"`.

----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-94634

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106074] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
                   ` (3 preceding siblings ...)
  2021-11-14  0:42 ` [ruby-core:106046] " sawa (Tsuyoshi Sawada)
@ 2021-11-16  0:49 ` shan (Shannon Skipper)
  2021-12-28 14:48 ` [ruby-core:106866] " joelb (Joel Blum)
  2021-12-29  4:38 ` [ruby-core:106889] " nobu (Nobuyoshi Nakada)
  6 siblings, 0 replies; 8+ messages in thread
From: shan (Shannon Skipper) @ 2021-11-16  0:49 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by shan (Shannon Skipper).


Control flow operators are another option that reverses the direction.

``` ruby
true and 'font-bold'
#=> "font-bold"

'font-bold' if true
#=> "font-bold"

nil and 'font-bold'
#=> nil

'font-bold' if nil
#=> nil
```


----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-94663

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106866] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
                   ` (4 preceding siblings ...)
  2021-11-16  0:49 ` [ruby-core:106074] " shan (Shannon Skipper)
@ 2021-12-28 14:48 ` joelb (Joel Blum)
  2021-12-29  4:38 ` [ruby-core:106889] " nobu (Nobuyoshi Nakada)
  6 siblings, 0 replies; 8+ messages in thread
From: joelb (Joel Blum) @ 2021-12-28 14:48 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by joelb (Joel Blum).


If we adopt this syntax it might be worthwhile to consider JS new colaescing operator which does the same thing, maybe it's better to keep the syntax similar to JS. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator.

So basically just

a ?? b

----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-95677

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:106889] [Ruby master Feature#18332] a ? b
  2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
                   ` (5 preceding siblings ...)
  2021-12-28 14:48 ` [ruby-core:106866] " joelb (Joel Blum)
@ 2021-12-29  4:38 ` nobu (Nobuyoshi Nakada)
  6 siblings, 0 replies; 8+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2021-12-29  4:38 UTC (permalink / raw
  To: ruby-core

Issue #18332 has been updated by nobu (Nobuyoshi Nakada).


`??` is a character literal.
As omitting the `:` and after makes the syntax value conflicting of course, I don't think this is acceptable.

----------------------------------------
Feature #18332: a ? b
https://bugs.ruby-lang.org/issues/18332#change-95694

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
It would be a shortcut for `a ? b : nil`:

e.g.

```
"#{current_path == "/" ? "font-bold"}"
"#{user.admin? ? "text-red-600"}"
```

Doing `a && b` returns `false` which converted to a string gives `"false"`.

When `nil` converts to a string it gives `""`.

I would use it mostly in string interpolations.



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-12-29  4:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-13 18:41 [ruby-core:106041] [Ruby master Feature#18332] a ? b dorianmariefr
2021-11-13 19:34 ` [ruby-core:106042] " zverok (Victor Shepelev)
2021-11-13 19:42 ` [ruby-core:106043] " dorianmariefr
2021-11-13 20:36 ` [ruby-core:106044] " zverok (Victor Shepelev)
2021-11-14  0:42 ` [ruby-core:106046] " sawa (Tsuyoshi Sawada)
2021-11-16  0:49 ` [ruby-core:106074] " shan (Shannon Skipper)
2021-12-28 14:48 ` [ruby-core:106866] " joelb (Joel Blum)
2021-12-29  4:38 ` [ruby-core:106889] " nobu (Nobuyoshi Nakada)

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