What is the difference between a constant and a variable in Ruby?

后端 未结 3 1698
野趣味
野趣味 2021-01-19 07:05

So, I\'m doing a Ruby course on CodeAcademy and I\'m stuck in differentiating the difference between a variable and a class. Can someone please explain the difference to me?

3条回答
  •  没有蜡笔的小新
    2021-01-19 07:41

    • A variable can change its value, it can vary.
    • A constant cannot change its value, it is constant.

    In Ruby things are a bit more complex though. You can reassign the value of constants, but it will print a warning. This is meant to be used for debugging only and the general principle still applies that constants are meant to be used for values that never change.

提交回复
热议问题