Why are symbols in Ruby not thought of as a type of variable?

前端 未结 8 1493
臣服心动
臣服心动 2020-11-30 12:50

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

相关标签:
8条回答
  • 2020-11-30 13:29

    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.

    0 讨论(0)
  • 2020-11-30 13:29

    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.

    0 讨论(0)
提交回复
热议问题