[#122900] [Ruby Bug#21529] Deprecate the /o modifier and warn against using it — "jpcamara (JP Camara) via ruby-core" <ruby-core@...>

Issue #21529 has been reported by jpcamara (JP Camara).

10 messages 2025/08/03

[#122925] [Ruby Feature#21533] Introduce `Time#am?` and `Time#pm?` — "matheusrich (Matheus Richard) via ruby-core" <ruby-core@...>

SXNzdWUgIzIxNTMzIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IG1hdGhldXNyaWNoIChNYXRoZXVzIFJp

10 messages 2025/08/06

[#122932] [Ruby Bug#21534] ppc64le Ractor ractor_port_receive aborted (core dumped) — "jaruga (Jun Aruga) via ruby-core" <ruby-core@...>

Issue #21534 has been reported by jaruga (Jun Aruga).

12 messages 2025/08/07

[#122953] [Ruby Bug#21540] prism allows `foo && return bar` when parse.y doesn't — "Earlopain (Earlopain _) via ruby-core" <ruby-core@...>

Issue #21540 has been reported by Earlopain (Earlopain _).

12 messages 2025/08/12

[#122964] [Ruby Feature#21543] Point ArgumentError to the call site — "mame (Yusuke Endoh) via ruby-core" <ruby-core@...>

Issue #21543 has been reported by mame (Yusuke Endoh).

8 messages 2025/08/14

[#122969] [Ruby Feature#21545] `#try_dig`: a dig that returns early if it cannot dig deeper — "cb341 (Daniel Bengl) via ruby-core" <ruby-core@...>

Issue #21545 has been reported by cb341 (Daniel Bengl).

10 messages 2025/08/15

[#122987] [Ruby Bug#21547] SEGV after 2083fa commit — "watson1978 (Shizuo Fujita) via ruby-core" <ruby-core@...>

Issue #21547 has been reported by watson1978 (Shizuo Fujita).

10 messages 2025/08/20

[#123042] [Ruby Feature#21550] Ractor.sharable_proc/sharable_lambda to make sharable Proc object — "ko1 (Koichi Sasada) via ruby-core" <ruby-core@...>

SXNzdWUgIzIxNTUwIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGtvMSAoS29pY2hpIFNhc2FkYSkuDQoN

16 messages 2025/08/21

[#123122] [Ruby Feature#21556] Add true? and false? methods to NilClass, TrueClass, FalseClass, and String — "Phalado (Raphael Cordeiro) via ruby-core" <ruby-core@...>

Issue #21556 has been reported by Phalado (Raphael Cordeiro).

9 messages 2025/08/29

[#123146] [Ruby Bug#21559] Unicode normalization nfd -> nfc -> nfd is not reversible — "tompng (tomoya ishida) via ruby-core" <ruby-core@...>

Issue #21559 has been reported by tompng (tomoya ishida).

8 messages 2025/08/31

[ruby-core:123108] [Ruby Feature#21515] Add `&return` as sugar for `x=my_calculation; return x if x`

From: "nhorton (Noah Horton) via ruby-core" <ruby-core@...>
Date: 2025-08-28 14:27:56 UTC
List: ruby-core #123108
Issue #21515 has been updated by nhorton (Noah Horton).


Earlopain (Earlopain _) wrote in #note-2:
> I would prefer it like this:
> 
> ```rb
> return result if result = my_calculation(input_a, input_b)
> ```
> 
> It doesn't need new syntax and to me at least it is rather intuitive. Unfortunatly this doesn't work today because the return code doesn't see the local that was declared in the if condition. You would have to write it as a multiline if or do `result = nil` above it which kinda defeats the point.

I agree that this syntax would be solid if it worked.

----------------------------------------
Feature #21515: Add `&return` as sugar for `x=my_calculation; return x if x`
https://bugs.ruby-lang.org/issues/21515#change-114425

* Author: nhorton (Noah Horton)
* Status: Open
----------------------------------------
Let me preface this by saying I have no marriage to the exact keyword name of `&return`.

# Problem
It is very common to have an early return in code where you get some initial value and return it if it is non-null. i.e.

```
return my_calculation(input_a, input_b) if my_calculation(input_a, input_b)
```

That form on its own is verbose and one where you need to look at it for a moment to confirm it is the same code on either side of the if.

If `my_calculation` is non-trivial at all, it normally gets turned into something with a variable:

```
my_calc = my_calculation(input_a, input_b)
return my_calc if my_calc
```

That is now two lines. The worse scenario, however, is if the user did not bother doing that and my_calculation turned out to be expensive (and they did not know it).

# Proposal
I propose a syntax of `&return my_calculation(input_a, input_b)` where it will evaluate the argument and return it as the result of the method if it is non-nil, otherwise it will continue on.

# Alternatives
## Do Nothing
There is no way to work around this with rolling your own methods. You can't make a `returnif` method or something yourself since you can't do a return in the caller's scope.

## Different Name

The best other name I saw were permutations of `returnif`. The biggest issue I see is the similarity of the following two statements:

```
return if foo
returnif foo
```

Those are obviously very, very different statements, but are somewhat similar. However, things like this are common, and the code is still quite distinct on those, so I think it is acceptable.

Ultimately, this feels so similar to the safe navigator that using `&` in this context feels appropriate.

Thank you all for your consideration.





-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/


In This Thread