New to programming and to Ruby, and I hope this question about symbols is in line. I understand that symbols in Ruby (e.g., :book
, :price
) are useful particularly as hash keys, and for all-around doing a lightweight, specific subset of the things that strings can do.
However, I am confused about symbols in one respect. Specifically, when they're used in the attr_accessor
types of methods, it appears that they are behaving more like a variable. E.g., attr_reader :book, :price
.
If true that they are variables in that usage, this is a bit puzzling because they are not typically listed among variable types (like the $global, @instance, local, @@class, and sometimes, CONSTANT, variable types) when variables types are described.
And if symbols are variables when used this way, what scope should be expected of them? Or are they still somehow lightweight strings in this context as well? (Or perhaps in some broader way, do symbols, strings, and variables all share a fundamental duck-like nature?) Thank you in advance for your insights and advice.
Variables give a label to an object. Symbols are more like strings, except that they're immutable and interned in memory, so that multiple references to the same symbol don't use extra memory.
Ruby symbols are defined as “scalar value objects used as identifiers, mapping immutable strings to fixed internal values.” Essentially what this means is that symbols are immutable strings. In programming, an immutable object is something that cannot be changed.
There are two main differences between String and Symbol in Ruby. String is mutable and Symbol is not: Because the String is mutable, it can be change in somewhere and can lead to the result is not correct. Symbol is immutable.
Because symbols are immutable, Ruby doesn't have to allocate more memory for the same symbol. That is because it knows that once it put the value in memory, it will never be changed, so it can reuse it. You can easily see this by looking at their object IDs.
Symbols are not variables, but a type of literal value, like numerals and quoted strings. Significantly, symbols are used to represent variables and other named values in the Ruby runtime. So when the Ruby interpreter sees the name foo
used as a variable or method name, what it looks up in the Hash of runtime values is the symbol :foo
, not the string "foo"
. This was, in fact, the original use of the term "symbol" in programming language terminology; variables, functions, constants, methods, and so on are said to be stored in the compiler or interpreter's "symbol table".
Pretty much any time you're passing around the name of something in Ruby, you're going to use a symbol. If you use method_missing
as a catch-all to implement arbitrary methods on your object class, a symbol is what it receives as an argument telling it the name of the method that was actually called. If you inspect an object with .methods
or .instance_variables
, what you get back is an array of symbols. And so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With