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.
上一項: 辭彙 索引 下一項: 字義