変数の先頭はアルファベット小文字。任意のオブジェクト(の参照番地)を代入可能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
number = 1 string = "壱" puts number puts string number = 2 string = "弐" puts number puts string number = string puts number # 1 # 壱 2# # 弐 # 弐 |
定数は先頭がアルファベット大文字。定数に値を代入しようとするとwarning。
1 2 3 4 5 6 7 8 9 10 11 12 |
Const = 1 Const = 2 CONST = "壱" CONST = "弐" puts Const, CONST # C:/Users/.../variable_and_constatnst.rb:2: warning: already initialized constant Const # C:/Users/.../variable_and_constatnst.rb:1: warning: previous definition of Const was here # C:/Users/.../variable_and_constatnst.rb:4: warning: already initialized constant CONST # C:/Users/.../variable_and_constatnst.rb:3: warning: previous definition of CONST was here # 2 # 弐 |