attr_reader :fname, :lname (attr_reader “fname”, “lname” works too)
knows how to map the names because that’s what an attribute is: A
read-only attribute ‘foo’ will have a getter method named ‘foo’ and an
instance variable ‘@foo’. It’s a common enough convention, used in
other languages as well. (In Java, it would be a method ‘getFoo()’ and
an instance variable ‘foo’.)
The difference between symbols and strings:
A string is a sequence of characters. You can append to a string,
parse it, split it, iterate over characters or lines and so forth. Two
strings containing the same character sequence (say “abc” and “abc”)
are equal, but not necessarily the same object.
Strings can be basically any character or byte sequence, like the
contents of a text or binary file. Strings are local and are garbage
collected when they are no longer referred to, like other objects.
A symbol is atomic, immutable and unique: It cannot be parsed or
modified, and all references to a symbol with a given name (say :abc)
refers to the same object.
Symbols tend to be short, simple names, like a single word with no
whitespace. Symbols are global, and hang around quite a bit longer
than strings normally do, often until the end of the program.
Symbols are (or can be) quicker for hash lookup, since it is
sufficient to compare object identity to find whether two symbols are
the same, while strings must be compared character by character. You
are unlikely to notice the difference unless your program uses hashes
heavily.
So they are not fundamentally the same. But there are a some cases
where they can be used interchangeably, like naming an attribute or as
hash key.
Reasons for using symbols instead of strings are mostly based on
convention. Personally, I use them basically because I save a
keystroke in typing them
Does this make it any clearer?
johannes