Is There a Definitive Reference Document for Ruby Syntax

Is there a definitive reference document for Ruby syntax?

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.

Where is Ruby's string literal juxtaposition feature officially documented?

UPDATE

This is now officially documented in the RDoc that ships with Ruby.

Changes will propagate to RubyDoc the next time they build the documentation.

The added documentation:

Adjacent string literals are automatically concatenated by the interpreter:

"con" "cat" "en" "at" "ion" #=> "concatenation"
"This string contains "\
"no newlines." #=> "This string contains no newlines."

Any combination of adjacent single-quote, double-quote, percent strings will
be concatenated as long as a percent-string is not last.

%q{a} 'b' "c" #=> "abc"
"a" 'b' %q{c} #=> NameError: uninitialized constant q

ORIGINAL

Right now, this isn't anywhere in the official ruby documentation, but I think it should be. As pointed out in a comment, the logical place for the docs to go would be: http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Strings

I've opened a pull request on ruby/ruby with the documentation added.

If this pull request is merged, it will automatically update http://www.ruby-doc.org. I'll update this post if/when that happens. ^_^

The only other mentions of this I've found online are:

  • The Ruby Programming Language, page 47 (mentioned in another answer)
  • Ruby Forum Post circa 2008
  • Programming Ruby

Is there a formal specification (like the Java Language Specification (JLS)) for Ruby?

Ruby 1.8 has been the subject of several industry standards. The language specifications for Ruby were developed by the Open Standards Promotion Center of the Information-Technology Promotion Agency (a Japanese government agency) for submission to the Japanese Industrial Standards Committee (JISC) and then to the International Organization for Standardization (ISO). It was accepted as a Japanese Industrial Standard (JIS X 3017) in 2011[24] and an international standard (ISO/IEC 30170) in 2012.[25]

References

  • Wikipedia/Ruby (programming language)

See also

  • ruby-std.netlab.jp - draft Ruby ISO standard

    • ruby-standard.org - a wiki-format mirror of the draft Ruby ISO standard
  • spec.ruby-doc.org - a community-driven project to write a complete, executable specification

Why are methods in Ruby documentation preceded by a hash sign?

From the rdoc docs (emphasis mine):

Names of classes, source files, and
any method names containing an
underscore or preceded by a hash
character
are automatically
hyperlinked from comment text to their
description.

Ruby confusion on the use of colons

In this case you are calling the function post with two parameters, the first parameter is the symbol :create and the second is a hash with the key :product and the value @update.

This line could be re-written as follows:

post(:create, {:product => @update})

The key: value style was introduced in Ruby 1.9.

What Is the correct syntax for writing test definitions in Cucumber?

I was planning to point you to online documentation, but the online documentation I know about (at cucumber.io and at relishapp.com) doesn't actually answer your question well. (It does contain many examples, though, and is well worth reading.)

In the Ruby implementation of Cucumber, step definition files are .rb files in the features/step_definition directory. They contain a series of calls to methods that each define an implementation of a Gherkin step. Here's an example:

Given /^there is a user named "(.*)"$/ do |username|
# code that creates a user with the given username
end

There are several methods that define steps: Given, When, Then, And and But. They all do exactly the same thing, and you can use any one to define any step. The best practice is to use the one that reads best with the step you're defining (never And or But).

The argument passed to the step-defining method is a regular expression intended to match one or more steps in Gherkin .feature files. The above example matches the following step:

Given there is a user named "Ade Tester"

("Ade Tester" could be anything).

The block passed to the step-defining method is run when Cucumber executes a step which the regular expression matches. It can contain any Ruby code you like.

Matching groups (enclosed in parentheses) in the regular expression are passed to the block as block parameters. The number of matching groups must match the number of block parameters, or you'll get an error. A common convention is to enclose matching groups that match strings in quotes to visually separate them from the fixed part of the step, as I did above, but this is purely convention and you can choose not to do it.

The regexp need not match the entire step by default. If you want a definition to match only the entire step you must enforce that in the regular expression, as I did in the example above with ^ and $. Do that unless you have a good reason not to. This step definition (without $)

Given /^there is a user named "(.*)"/ do |username|
create :user, username: username
end

would match

Given there is a user named "Ade Tester" on weekdays but "Dave Schweisguth" on weekends

which would probably be a bad idea. Worse, if you had definitions for both steps, Cucumber would not be able to tell which definition to use and you'd get an error.


In features/step_definitions/documentation.rb:

When /^I go to the "([^"]+)" documentation$/ do |section|
path_part =
case section
when "Documentation"
"documentation"
else
raise "Unknown documentation section: #{section}"
end
visit "/documentation/#{path_part}/topics"
end

Then /^I should see the "([^"]+) documentation"$/ do |section|
expect(page).to have_css('h2.doctag_title a', text: section)
end

These steps exercise a web application. They are about as simple as they can be while still being practical.

Is it possible to get Ruby syntax highlighting in PHPStorm?

Ruby plug-in that you have linked is designed for IntelliJ IDEA Ultimate only, it will not work with PhpStorm. See this answer for more details.

However, it's possible to get the basic syntax highlighting for Ruby files in PhpStorm using the TextMate bundles support plug-in. It's already included with PhpStorm 6.0.1 and you don't need to install it, just make sure it's enabled in Settings | Plugins.

  1. Git clone Ruby.tmbundle into some directory.

  2. Add this directory in Settings | TextMate Bundles:

Ruby bundle

For some reason PhpStorm TextMate Bundles support will not recognize *.rb files as supported by this bundle. To fix this problem open Ruby.tmbundle\Syntaxes\Ruby.plist file in some text editor, find <key>fileTypes</key> section, add <string>rb</string> under <array>

(the above should be fixed in the latest Ruby bundle version, so the editing the bundle is no longer needed, but if you are adding some other language bundle, it's something you may need to adjust)

Restart PhpStorm, verify that *.rb is now associated correctly:

association

Now you get Ruby syntax highlighting in PhpStorm:

Ruby syntax


If you need full support for both Ruby and PHP (plus much more) in a single IDE, consider using IntelliJ IDEA Ultimate.

Is there a Javascript equivalent to the Ruby syntax using underscores (e.g. 10_000 = 10000) to make larger integers human readable?

1) No, there is not. At least not the way ruby does it. If you're familiar with scientific notation/E notation, you can turn a large number like 1000000000000000 into something more friendly like 1E15

As a sidenote: There has been a long-going discussion for coffeescript to include something similar to the ruby syntax here

2) It is called an underscore and has no special name in its role in Ruby integer literals. The only thing special in Ruby (and in Perl, Frink, Caml and others) is that underscores in integer literals are ignored. This feature is very old and was first used in Ada to make integer literals more readable.



Related Topics



Leave a reply



Submit