M
maghac
Hi,
as a long-term perl user just recently converted to ruby, I'm curious
about one particular syntax "feature": ruby symbols and how they
relate to strings.
Isn't really :name a shortcut for "name"? (I read somewhere in an
explanation of attr_reader that e.g :age was the "name" of the
variable age, while age is the actual content of that variable). If
so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
instead of hashvar['key'].
Are there situations where you cannot use symbols instead of strings,
or the other way around?
I might be too used to the way strings and barewords are handled in
perl (if something looks like a string, and it's not a function call,
it's interpreted as a string. This means you can say $hashvar{key}
without quoting the key).
as a long-term perl user just recently converted to ruby, I'm curious
about one particular syntax "feature": ruby symbols and how they
relate to strings.
Isn't really :name a shortcut for "name"? (I read somewhere in an
explanation of attr_reader that e.g :age was the "name" of the
variable age, while age is the actual content of that variable). If
so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
instead of hashvar['key'].
Are there situations where you cannot use symbols instead of strings,
or the other way around?
I might be too used to the way strings and barewords are handled in
perl (if something looks like a string, and it's not a function call,
it's interpreted as a string. This means you can say $hashvar{key}
without quoting the key).