Project

General

Profile

Feature #6023

Add "a ?= 2" support for meaning "a = a.nil? ? 2 : a"

Added by Rodrigo Rosenfeld Rosas about 4 years ago. Updated over 3 years ago.

Status:
Rejected
Priority:
Normal
[ruby-core:42630]

Description

I've just proposed this idea to Groovy and I thought the same semantics would be interesting to have in Ruby too:

http://jira.codehaus.org/browse/GROOVY-5306

This is a minor, but important, difference to the "a ||= 2" syntax.

This would be a caching/memoization operator, and it would allow code like this:

a = nil
a ?= false # a is false now
a ?= true # a is still false

This contrasts with

a = nil
a ||= false # a is false now
a ||= true # a is true now


Related issues

Duplicated by Ruby trunk - Feature #6561: ?= operator Rejected 06/09/2012

History

#1 [ruby-core:42747] Updated by Magnus Holm about 4 years ago

I've just proposed this idea to Groovy and I thought the same semantics would be interesting to have in Ruby too:

http://jira.codehaus.org/browse/GROOVY-5306

This is a minor, but important, difference to the "a ||= 2" syntax.

This would be a caching/memoization operator, and it would allow code like this:

a = nil
a ?= false # a is false now
a ?= true  # a is still false

This contrasts with

a = nil
a ||= false # a is false now
a ||= true  # a is true now

If we want something like this, we should provide a non-assignment
version too. Perl uses // for the same purpose:

sub foo {
my ($foo, %options) = @_;
$foo //= 1;
my $bar = $options{bar} // 2;
return ($foo, $bar)
}

foo(undef, bar => undef) # => (1, 2)
foo(0, bar => 0) # => (0, 0) (0 is false in Perl)

Although I suspect we rather want to use // for float/exact-division
in the future.

#2 [ruby-core:42751] Updated by Anonymous about 4 years ago

Magnus Holm judofyr@gmail.com writes:

If we want something like this, we should provide a non-assignment
version too. Perl uses // for the same purpose:

sub foo {
my ($foo, %options) = @_;
$foo //= 1;
my $bar = $options{bar} // 2;
return ($foo, $bar)
}

foo(undef, bar => undef) # => (1, 2)
foo(0, bar => 0) # => (0, 0) (0 is false in Perl)

Although I suspect we rather want to use // for float/exact-division
in the future.

How about '??' ?

#3 Updated by Shyouhei Urabe about 4 years ago

  • Status changed from Open to Assigned

#4 [ruby-core:45583] Updated by Robert A. Heiler almost 4 years ago

The perl example is not very elegant.

When I see code like this:

$foo //= 1;

I first think that someone wants to divide via / somehow.

The:

a ?= true # a is still false

looks a bit weird. Was ? not used to get the ASCII value of
characters before?

I also rarely see x = x ? y and it reminds me of ternary operator.

#5 [ruby-core:45625] Updated by Vlad Moskovets almost 4 years ago

I think #6561 more convenient to store nil'able and false'able items

#6 [ruby-core:48256] Updated by Yutaka HARA over 3 years ago

  • Target version changed from 2.0.0 to next minor

#7 [ruby-core:48297] Updated by Nobuyoshi Nakada over 3 years ago

  • Status changed from Assigned to Rejected

Also available in: Atom PDF