Ruby Variable Scope 简单讲解
生活随笔
收集整理的這篇文章主要介紹了
Ruby Variable Scope 简单讲解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| $ | A global variable |
| @ | An instance variable |
| [a-z]?or?_ | A local variable |
| [A-Z] | A constant |
| @@ | A class variable |
以一個簡單例子示例各種變量的區別: class Female# ConstantSEX = 'female'# Class variable: shared among all instances of this class@@sex = SEXdef initialize( weight, height)# Instance variable: accessible to specific instance of this class@weight = weight@height = heightenddef self.sex@@sexenddef description# Local variable: local to this blockideal_weight = @height * 0.8puts 'This female ideal weight would be ' + ideal_weight.to_s + ' and her actually weight is ' + @weight.to_sendendputs Female.new(130, 170).description puts Female.sex
總結
以上是生活随笔為你收集整理的Ruby Variable Scope 简单讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PhpWord的autoload.php
- 下一篇: 解决LSP问题导致无法上网