7 points · by izietto · 13y ago · 19 comments
RubySpec, for those who aren't familiar with it, is an attempt to write an executable specification for the language. Ruby's definitely suffered from not having this in the past. https://github.com/rubyspec/rubyspec/ and specifically case statement spec at https://github.com/rubyspec/rubyspec/blob/master/language/ca...
Most functional languages have, I think, somewhat better systems in that there's less magic[0], you can unwrap/extract values and you can add further conditions on values (guards)[1][2][3].
Some modern multiparadigm languages have lifted this style e.g. Scala or Rust.
[0] because case/when relies on the non-commutative `===` its behavior can be surprising
[1] http://learnyouahaskell.com/syntax-in-functions#case-express...
[2] http://learnyousomeerlang.com/syntax-in-functions#in-case-of
[3] http://en.wikibooks.org/wiki/F_Sharp_Programming/Pattern_Mat...
Also, it's not a simple control structure, Ruby's case/when does not do equality checks (via ==) it does match checks (via ===) so you can match integers to ranges:
> (1..3) === 2
=> true
or instances to classes > String === "foo"
=> true
But `===` is not commutative: > 2 === (1..3)
=> false
and the matches can seem somewhat arbitrary: > (1..5) === (2..3)
=> false
[0] http://www.techotopia.com/index.php/The_Ruby_case_Statementmade my day, thanks.
Java: http://stackoverflow.com/questions/11227809/why-is-processin...
Php: http://stackoverflow.com/questions/60174/how-can-i-prevent-s...
Javascript: http://stackoverflow.com/questions/111102/how-do-javascript-...