Is there a definitive reference document for Ruby syntax? [closed]

若如初见. 提交于 2019-12-03 09:53:06

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.

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.

duncan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!