21 comments

[ 2.7 ms ] story [ 60.3 ms ] thread
Leads to "How can I write a switch statement in Ruby?"
Yes, it seems to me quite ironic (and weird) that the most voted Ruby question on StackOverflow asks for how to write a switch!
What's ironic about it? What would you expect to be the most voted ruby question?
Surely not a question which could be easily answered with a LMGTFY like this http://lmgtfy.com/?q=switch+statement+in+ruby
That's exactly why it would be the most voted question. It's a common thing to Google, and the Google search leads to the StackOverflow page linked, which leads to lots of upvotes.
Yes, but if I want informations about Ruby switch statements it means you are a newbie that should refer to a whole language syntax reference; you could say that Ruby has not an official language syntax reference (this is the most "official": http://web.njit.edu/all_topics/Prog_Lang_Docs/html/ruby/synt... ) so newbies are forced to google for syntax explanations, which is true, but it seems too little to make it the first Ruby question.

And I think Ruby management has its negligence; it's weird that a so popular and mature language has so much lack of informations about its behaviour.

That question was...unexpected to me, to say the least. I was expecting something obscure, maybe related to meta programming, certainly not something about flow structures!
You were expecting something obscure to be the most voted question?
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)

How on earth did this get to the front page? :D
Is there no equivalent of php.net that explains this simple control structure?
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
> The ruby community does not really believe in documentation

made my day, thanks.

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.
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!"

(comment deleted)
Is this perhaps because ruby calls it a case statement instead of a switch statement?
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/ and specifically case statement spec at https://github.com/rubyspec/rubyspec/blob/master/language/ca...

> 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...

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

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

I think the poster was trying to be snarky but so far this has been unacknowledged