Ruby 學習筆記-變數&常數(Variable)
1.Ruby 變數種類

2.變數指定
a = a + 2
一個等號是指定

也可以寫成這樣

3.變數多重指定
a.可以利用 Ruby 變數多重指定特性寫成這樣
x,y,z = 1,2,3
b.如果等號的右邊是陣列也可以自動拆解出來
x,y,z = [1,2,3]
4.變數的命名慣例
Ruby 世界裡有不成文規定

5.常數命名
常數必須要是大寫英文字母開頭
Luna = 1
在寫程式時需要給變數一個好名字
具有清楚與有意義(給 x,y,z 不能使人清楚這段程式碼在寫什麼)
變數與常數就像標籤本身沒有型態
可以把它想像成是「一張有寫著名字的標籤,貼在某個東西上面」,被貼的那個東西有形態,但標籤本身沒有。
comments powered by
This site uses cookies. By continuing to use this website, you agree to their use. I Accept