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 usef
They aren't variables because they don't hold values, they are immutable. The thing is the value itself. It's similar to numbers. You can't set the value of 1
. 1 = 2
doesn't work.
Ruby's attr_accessor
, attr_reader
, and attr_writer
are just shorthand ways of avoiding writing a bit of repetitive code. The following question expands on how these work: Why use Ruby's attr_accessor, attr_reader and attr_writer?
Instead of thinking of attr_reader :book
as a variable, just think of it as a name of an attribute that is specified using a symbol.