

The most voted Ruby question on Stack Overflow - izietto
http://stackoverflow.com/questions/948135/how-can-i-write-a-switch-statement-in-ruby

======
matthewmacleod
Ruby's case statements are super-awesome, and I constantly find myself longing
for similar sugar elsewhere. Obviously it's more useful in a duck-typed
language.

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/](https://github.com/rubyspec/rubyspec/)
and specifically case statement spec at
[https://github.com/rubyspec/rubyspec/blob/master/language/ca...](https://github.com/rubyspec/rubyspec/blob/master/language/case_spec.rb)

~~~
masklinn
> Ruby's case statements are super-awesome

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...](http://learnyouahaskell.com/syntax-in-functions#case-expressions)

[2] [http://learnyousomeerlang.com/syntax-in-functions#in-case-
of](http://learnyousomeerlang.com/syntax-in-functions#in-case-of)

[3]
[http://en.wikibooks.org/wiki/F_Sharp_Programming/Pattern_Mat...](http://en.wikibooks.org/wiki/F_Sharp_Programming/Pattern_Matching_Basics)

------
x3ro
How on earth did this get to the front page? :D

------
stef25
Is there no equivalent of php.net that explains this simple control structure?

~~~
masklinn
The ruby community does not really believe in documentation, so you have to go
for third-party sources[0] or books

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_Statement](http://www.techotopia.com/index.php/The_Ruby_case_Statement)

~~~
k__
> The ruby community does not really believe in documentation

made my day, thanks.

~~~
edoloughlin
The lack of documentation, especially when you want to do something non-
standard, made me give up on RoR a few years ago. I got sick of getting lost
when looking at the source, which seemed to consist of 5-10 line methods with
a call hierarchy that was usually over 10 methods deep.

~~~
k__
Never tried Ruby or RoR.

But "I don't believe in ..." or "She doesn't believe in ..." is a running gag
in my circle of friends.

"He got rather bad teeth so he had to pay much money for getting them done..."
\- "Didn't his insurance cover this?" \- "He doesn't believe in insurance!"

------
eliot_sykes
Leads to "How can I write a switch statement in Ruby?"

~~~
izietto
Yes, it seems to me quite ironic (and weird) that the most voted Ruby question
on StackOverflow asks for how to write a switch!

~~~
dasil003
What's ironic about it? What would you expect to be the most voted ruby
question?

~~~
pgsandstrom
Just compare it to other languages, there is a clear difference.

Java: [http://stackoverflow.com/questions/11227809/why-is-
processin...](http://stackoverflow.com/questions/11227809/why-is-processing-a-
sorted-array-faster-than-an-unsorted-array)

Php: [http://stackoverflow.com/questions/60174/how-can-i-
prevent-s...](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-
injection-in-php)

Javascript: [http://stackoverflow.com/questions/111102/how-do-
javascript-...](http://stackoverflow.com/questions/111102/how-do-javascript-
closures-work)

~~~
Shish2k
Looking at the top few questions for several languages, I note some trends:

Ruby, Python, Javascript: unique-ish language features

Java: general comp-sci corner cases

C, C++: what does this obscure operator mean

Erlang, Haskell, Node: how to get started

Go: how to live without features that were intentionally not included

PHP: How to avoid the security pitfalls that the standard library encourages

Seems about right... (Though I'm surprised to see Node alongside Erlang and
Haskell in terms of people not knowing where to start)

------
achivetta
Is this perhaps because ruby calls it a case statement instead of a switch
statement?

