Feature #12519


Expansion of modifier_if and modifier_unless

Added by bk5115545 (Blaise Koch) almost 5 years ago. Updated almost 5 years ago.

Target version:


Hi all.

I propose a syntax change to allow the below to be valid ruby.

variable = 1 if false else 2

This is an extension of the current modifier_if below which accomplishes the same result.

variable = 1 if false
variable = 2 if !false

keeping up with the other condition-statement keyword the below should be valid as well.

variable = 1 unless true else 2

And is an extension of

variable = 1 unless true
variable = 2 unless !true

While it's true that ternary syntax accomplishes the same task, I feel that the proposed changes are both more readable and a more natural extension of the language.

Updated by phluid61 (Matthew Kerwin) almost 5 years ago

I am against.

It seems to rewrite the precedence of modifier if. I would expect it to evaluate like this:

false ? (variable = 1) : 2

Also does it only apply to assignment? How about other method calls? What would happen in all of the following cases?

a = b = c = 1 if false else 2
[0][0] = 1 if false else 2
[0].[]= 0, 1 if false else 2
a, b = 0, 1 if false else 2
p 1 if false else 2

It's too confusing.

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • Description updated (diff)

Matthew Kerwin wrote:

It's too confusing.


Updated by shevegen (Robert A. Heiler) almost 5 years ago

Yes, confusing and also not very pretty.

Updated by darix (Marcus Rückert) almost 5 years ago

variable = true ? 1 : 2

isnt that short enough?

Actions #5

Updated by matz (Yukihiro Matsumoto) almost 5 years ago

  • Status changed from Open to Rejected

Agreed to Marcus.



Also available in: Atom PDF