文森說技術

iOS, Web Development Notes
- , ,

Ruby 中變數的種類

Ruby 中的變數有四種,根據有無 prefix 和 prefix 種類不同,各種的作用域及功能都不一樣。

  • 無 prefix :普通的 local variable , scope 根據所在位置而定。
  • 有 prefix
    • @ : instance variable , based on its class ,無法由外部存取
    • @@ : class variable ,屬於 class 的 attribute
    • $ : global variable ,全域的變數。
如果覺得這篇對你有幫助,歡迎幫忙分享給其他人 😀