Ruby Programming/Syntax/Variables and Constants

维基教科书,自由的教学读本
上一项: 辞汇 索引 下一项: 字义

在 Ruby 中,我们可以识别字为一个变数命名,名称没有长度限制。一个尚未初始之变数,其值为nil

区域变数(Local Variable)[编辑]

范例:

foobar

一个变数之名称若以小写英文字母 (a-z) 或底线字元 (_) 为首,则该变数即为区域变数或方法之参数。区域变数只能在它初始化之区块内存取。例如:

i0 = 1
loop {
  i1 = 2
  print defined?(i0), "\n"	# true; 已定義,"i0" 於上層區塊中初始化。
  print defined?(i1), "\n"	# true; 已定義,"i1" 於此區塊中初始化。
  break
}
print defined?(i0), "\n"	# true; 已定義,"i0 於此區塊中初始化。
print defined?(i1), "\n"	# false; 未定義,"i1" 於 loop 區塊中初始化。

实例变数(Instance Variable)[编辑]

范例:

@foobar

一个变数之名称若以 '@' 为首,即为实例本身的实例变数。实例变数属于实例之一部份,其意义等于 C++/Java 语言中之私有资料成员(private data member)。事实上,Ruby 严格遵守一个变数只能在其初始化区块中存取之定义,因此区块以外之叙述不允许存取实例内之变数。若外部想存取实例变数,只能透过实例的方法

类别变数(Class Variable)[编辑]

范例:

@@foobar

在 Ruby 中也是一个实在的个体,因此类本身也有内部的变数。其意义等于C++/Java语言中之类别私有静态成员(private static class member)。

全域变数(Global Variable)[编辑]

一个变数之名称若以 '$' 为首,即属全域变数;当程式运作时,可在任何地方存取它。 范例:

$foobar

常数(Constant)[编辑]

范例:

FOOBAR

一个变数之名称若以大写英文字母 (A-Z) 为首,即为常数。Ruby 允许在初始化常数后又再次指派其他值,但会发出警告讯息。Ruby 的每个都是一个常数。

若试图取得一个未初始常数之值,将会唤起一个NameError异常。

虚设变数(Pseudo Variable)[编辑]

虚设变数具有变数的性质,但其意义固定,由 Ruby 按其意义决定虚设变数指涉的内容。若改变虚设变数之意义,将令 Ruby 发出异常。

self

指涉行为个体本身。和 C/C++ 语言之 this 关键字意义相同。

nil

表示“无此物”,这是 NilClass 类别的唯一实例。

true

“真”或“真值”,为 TrueClass 类别之唯一实例。

false

“伪”或“伪值”,为 FalseClass 类别的唯一实例。 (在 Ruby 中,nil 也视同 false,除此以外任何值都视同 true。)

预先定义变数(Pre-defined Variable)[编辑]

异常相关:

$!         raise設定的異常資訊訊息。
$@         最近被擲出之異常的追蹤資訊陣列。

与最近字样比对动作相关:

$&         符合字樣之內容。
$`         符合字樣之字串左側的內容。
$'         符合字樣之字串右側的內容。
$+         符合最後一個字樣分組的內容。
$1 to $9   符合第 1 到 9 個字樣分組的內容。
$~         比對動作相關資訊。
$=         The flag for case insensitive, nil by default.
$/         The input record separator, newline by default.
$\         The output record separator for the print and IO#write. Default is nil.
$,         The output field separator for the print and Array#join.
$;         The default separator for String#split.
$.         The current input line number of the last file that was read.
$<         The virtual concatenation file of the files given on command line.
$>         The default output for print, printf. $stdout by default.
$_         The last input line of string by gets or readline.
$0         手稿程式的名稱。可指派。
$*         命令列引數陣列,即 ARGV 。
$$         運行手稿程式的 Ruby 行程識別碼(PID)。
$?         最後執行的子行程之狀態碼。
$:         載入(load or require)手稿程式檔案或模組時之搜尋路徑的陣列。
$"         以 require 載入的模組名稱之陣列。
$DEBUG     The status of the -d switch.
$FILENAME  Current input file from $<. Same as $<.filename.
$LOAD_PATH $: 的別名。
$stderr    現行標準錯誤輸出設備。
$stdin     現行標準輸入設備。
$stdout    現行標準輸出設備。
$VERBOSE   The verbose flag, which is set by the -v switch.
$-0        $/ 的別名。
$-a        True if option -a ("autosplit" mode) is set. Read-only variable.
$-d        $DEBUG 的別名。
$-F        $; 的別名。
$-i        In in-place-edit mode is set, this variable holds the extension, otherwise nil.
$-I        $: 的別名。
$-l        True if option -l is set ("line-ending processing" is on). Read-only variable.
$-p        True if option -p is set ("loop" mode is on). Read-only variable.
$-v        $VERBOSE 的別名。
$-w        True if option -w is set.
上一项: 辞汇 索引 下一项: 字义