Admittedly, I'm having a terrible time understanding what Ruby wants
with respect to parentheses, #{} constructs, and the like. It's very
confusing.
Here's a short summary of how those things are used in ruby. I hope you'll
find it useful:
- parentheses: they have two uses:
* method calls: when you call a method, you put its arguments in parentheses,
taking care not to put spaces between the method name and the parentheses. If
you want, you can usually (but not always), omit parentheses in this case
* grouping, like in 2 * (3 + 2 )
- double quotes (""): they are the delimiters of double quoted strings. In
this kind of string, string interpolation (that is, the #{} construct) happens
and the backslash (\) character has a special meaning (see the table
"Substitutions in double-quoted strings" in http://www.ruby-
doc.org/docs/ProgrammingRuby/html/language.html). If you need to put a double
quote character inside a double quoted string, you need to prefix it with a
backslash, this way: "a \"double quoted\" string"
- single quotes (''): they are the delimiters of single quoted strings, where
string interpolation doesn't happen and the backslash has no special meaning,
except when it precedes a single quote (in this case, it allows to insert a
literal single quote into the string)
- string interpolation (#{}): delimits a piece of ruby code embedded in a
double-quoted string. The code is run and the value it returns is put in the
string in place of the code itself. (Note that this happens as the string is
created, it can't be delayed)
NOTE: technically, single-quoted and double-quoted strings are not two
different strings, but different kinds of string literals, that is two
different ways of writing a string in your program.
If the summary above doesn't help you, you can try looking at the "Programming
ruby", whose first edition is freely availlable online
(Programming Ruby: The Pragmatic Programmer's Guide), expecially at the chapter
"The Ruby Language", and ask specific question on those things which confuse
you here.
My scripts are a horrible mess because I can't get things to work
consistently, and almost every time I refer to a variable, a directory,
or a file, I have to write a mini-script just to figure out what it
wants.
If you need to test short pieces of code, you can use irb.
Stefano
···
On Tuesday 12 August 2008, Thomas Luedeke wrote: