views:

127

answers:

4

I'm searching for a definitive document on Ruby syntax. I know about the definitive documents for the core API and standard library, but what about the syntax itself? For instance, such a document should cover: reserved words, string literals syntax, naming rules for variables/classes/modules, all the conditional statements and their permutations, and so forth.

I know there are many books and tutorials, yes, but every one of them is essentially a tutorial, each one having a range of different depth and focus. They will all, by necessity of brevity and narrative flow, omit certain details of the language that the author deems insignificant.

For instance, did you know that you can use a case statement without an initial case value, and it will then execute the first true when clause? Any given Ruby book or tutorial may or may not cover that particular lesser-known functionality of the case syntax. It's not discussed in the section in "Programming Ruby" about case statements. But that is just one small example.

So far the best documentation I've found is the rubyspec project, which appears to be an attempt to write a complete test suite for the language. That's not bad, but it's a bit hard to use from a practical standpoint as a developer working on my own projects.

Am I just missing something or is there really no definitive readable document defining the whole of Ruby syntax?

+1  A: 

This is a pretty good Ruby syntax resource

http://www.cs.auckland.ac.nz/references/ruby/doc_bundle/Manual/man-1.4/syntax.html

macek
Hm yes, I've found that document useful as well from a practical standpoint, but I was not able to find any kind of authoritative parent resource that gave it credibility as a reference document. So I wasn't sure how much I could trust it, or how much the language has changed since 1.4 for that matter.
JSW
A: 

As the answer above suggested, there is an effort on writing a "spec" on http://ruby-std.netlab.jp.

You have to be careful with that "spec". It is not done by the language designers, and they are basically documenting what they see and not the other way around. It is based on 1.8.7 therefore there may be features in 1.9 that are not covered.

I can't imagine myself remembering the syntax with a 300 page document. To have "over the table", this cheatsheet describes what you want: condensed syntax, reserved words, etc. This is an alternative from dzone.

duncan
I was under the impression that the spec was officially sanctioned by the Ruby devs (Matz et al.). Is that wrong? And, yes, the choice of 1.8.7 was controversial, but it's a start. See here for further info and links: http://www.rubyinside.com/ruby-iso-spec-draft-2900.html
Telemachus
The spec is led by Shugo Maeda (matz's right hand man), Shyouhei Urabe (maintainer of Ruby 1.8.7 and MRI 1.8.7) and Yutaka Hara (Ruby, YARV and MRI developer) in close collaboration with matz himself. And it's not based on 1.8.7, it's based on the common subset of 1.8.7 and 1.9.2. You are confusing the spec with the process (which is ironic since that is exactly what *you* accuse the authors of the spec of): they started from 1.8.7 and removed everything that is not in 1.9.2. They could have done it the other way round, the result would be exactly the same.
Jörg W Mittag
Jörg, thanks for that information.
duncan
I just re-read my comment and realized that it might come off too harsh. I apologize for that. As you can probably guess from my name, English is not my first language. I guess I got rather frustrated with the misinformation about the ISO Ruby process. Of course, most of that misinformation is caused by the fact that there isn't actually that much *correct* information, either, as most of the process takes place in a manner that cannot exactly be described as "transparent". Oh, and the fact that discussion mostly happens in Japanese doesn't help much.
Jörg W Mittag
+4  A: 

There is a draft of a Ruby standard in the works. You can get it here: http://ruby-std.netlab.jp/

Your example about case and Programming Ruby is poorly chosen. When Programming Ruby introduces case (on page 98 for the first edition; 141 for the second edition), Thomas says,

The Ruby case expression is a powerful beast: a multiway if on steroids. And just to make it even more powerful, it comes in two flavors.

He then briefly explains both ways to use case (with an explicit target after the initial case and without). He actually begins with the style you say he doesn't mention.

Your larger point is not unreasonable though. It will probably be helpful to have a standard.

Telemachus
Note that the ISO specification is very minimal. It only covers the common subset of 1.8 and 1.9 (so, for example, no stabby proc and no symbol hashes). And it doesn't even cover the whole common subset, only a very small part.
Jörg W Mittag
Regarding case statement and the Programming Ruby book, I'd been looking at the version hosted from ruby-doc.org which is labeled as the first edition from the ruby-doc homepage. That particular version does not happen to mention the "two flavors" of case statments (http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#S5) In any case, you are right that the case statement was a bad example! Different versions of the book abound, and ultimately that only adds weight to the need for a standard reference it would seem.
JSW
+3  A: 

The only document that can be reasonably described as "definitive" is the source code of parse.y in the YARV source tree.

The ISO Draft Specification contains a 39 page appendix with a summary of the grammar. Note, however, that ISO Ruby is a minimal subset of the intersection of Ruby 1.8 and 1.9. IOW: it doesn't describe anything that is only in 1.8 or only in 1.9 (so, the syntax additions in 1.9 like stabby proc and symbol hashes aren't described), nor does it describe everything in that intersection. ISO Ruby is a bit like ISO HTML in that regard.

The RubySpec project contains executable specifications for the Ruby language. It doesn't contain an explicit specification of the grammar, though. The only specification of the grammar is implicit in the examples themselves. Also, because RubySpec is an example-based spec, it can only show you specific examples of valid Ruby code, but it cannot tell you all possible valid Ruby programs like a grammar spec could. And, because RubySpec is itself executable Ruby code, it can only show you valid examples, not invalid ones.

The last thing that could be considered definitive is the book The Ruby Programming Language by David Flanagan and Yukihiro "matz" Matsumoto.

Note, however, that "the whole of Ruby syntax" is a rather daunting task, because Ruby's syntax is insanely complicated with a ginormous amount of weird corner cases.

Jörg W Mittag
@Jörg W Mittag: Thanks for the clarifications about the ISO draft. I have to say that the business about the common subset of 1.8 and 1.9 is not at all clear from the draft's website (or the press it's been getting). In the Drafting Guidelines section, the authors of the website say (twice) that they are using 1.8.7 as the primary reference. Like most people, I took this to mean that 1.8.7 was the target they were aiming to specify.
Telemachus
@Telemachus: It's hidden in the Drafting Guidelines: Second, we intend that existing implementations such as Ruby 1.8.7, Ruby 1.8.6, **Ruby 1.9**, JRuby, Rubinius, and IronRuby can conform to the specification **without modifying them**. There are some features which are not implemented in some of the implementations or are different among the implementations. **Those features are excluded from the specification, or described as either "implementation defined" or "implementation dependent."**
Jörg W Mittag