User:WillWare/Learning Ruby

I've been starting to learn Ruby on Rails and I realize I need a few notes about the Ruby language itself.

Ruby tutorials

 * http://www-users.math.umd.edu/~dcarrera/ruby/0.3/
 * http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm
 * http://www.meshplex.org/wiki/Ruby/Ruby_on_Rails_programming_tutorials2

Variable name conventions

 * No leading punctuation indicates a local variable of the function or method. It lasts only until you leave the function or method.
 * A single at-sign (e.g. "@data") indicates an instance variable. Each instance of the class has its own value for this variable.
 * A double at-sign (e.g. "@@sharedData") indicates a class variable, shared by all instances of the class.
 * A dollar sign (e.g. "$DATA") indicates a global variable.
 * A name beginning with a capital letter (e.g. "PI") indicates a constant.

Blocks
Much of Ruby's syntax is pretty obvious by comparison with other languages, but one piece of syntax is worth a look. This adds up the even numbers from 4 to 12, yielding 40.
 * The first tricky thing is "4.step(12, 2)", which constructs a list starting with 4, incrementing by 2, and ending with 12.
 * The second tricky thing is that defines a block, sort of an anonymous function, whose argument is x.
 * The last piece is "to_s", which is an explicit cast of an object to a string.

Functions return the last expression
Like Lisp, Ruby doesn't distinguish between statements and expressions. If a function or method has a series of expressions, the last one becomes the return value, like the "let" construct in Scheme. If there are more Lisp/Scheme-like constructs in Ruby, I'm going to like it.

Some things are still mysterious
What is going on here? Are we passing three separate arguments to map.resources, or a single hash with three keys? I tried this. The ":x" is taken as the first argument, and the rest is taken as a hash with two keys. So we're passing in two arguments, a symbol and a hash. This gives an error because ":y" is not continuing the hash.