Project

General

Profile

Feature #10343

Postfix notations for `when` and `else` inside `case` statement

Added by sawa (Tsuyoshi Sawada) about 5 years ago. Updated about 5 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:65528]

Description

In case statements, the condition part are not always uniform in length, and especially, else is short. When we want to put each when statement in a single line, they are not always aligned, and are hard to read.

case foo
when some_very_long_condition then "a"
when short_cond then "bb"
when some_long_condition then "ccc"
else "dddd"
end

I think it would be better if we can do postfix notations with when and else inside case statement as below.

case foo
  "a"    when some_very_long_proc
  "bb"   when short_regex
  "ccc"  when some_long_regex
  "dddd" else
end

The length of "a", "bb", "ccc", "dddd", etc. can also vary, but they are usually more homogenuous with respect to length than the condition, and easier to align. And, it is these values that we are interested in, rather than the conditional parts. Furthermore, this notation is closer to the case-like notation standardly used in mathematics for conditions, so it would be easier to read for those who are familiar with mathematics.

x! = 1          (x = 0)
     x (x - 1)! (otherwise)

History

Updated by gogotanaka (Kazuki Tanaka) about 5 years ago

I'm not sure how we can implement this.. but it looks not bad for me.

What I afraid of most is ..being able to parse this syntax well?

Aside from that, we can achieve what you want for now.

case foo
when some_very_long_condition then "a"
when short_cond               then "bb"
when some_long_condition      then "ccc"
else                               "dddd"
end

I have to admire this looks little bit ugly : (

Thank you.

Also available in: Atom PDF