你 可以至 Ruby 的官方網站 下載安裝Ruby,初學 Ruby,可以執行Ruby安裝目錄中的irb指令,啟動指令互動環 境來作些簡單的程式練習,可以自行進入文字模式,設定PATH中包括Ruby安裝目錄的bin目錄,再執行irb指令以進入指令互動環境:
> irb
irb(main):001:0> 1 + 2
=> 3
irb(main):002:0>
irb(main):001:0> 1 + 2
=> 3
irb(main):002:0>
這是Ruby的指令互動環境,可以讓你很快地撰寫一些小指令進行測試(經常的,你只是要看看某個指令這麼用對不對,或會有什麼結果),上例執行1 + 2,=> 3表示結果為3,如果想離開irb,可以輸入exit。
預設irb提示(Prompt)較為冗長,可以執行irb時加上--simple-prompt,顯示簡單提示字元,順便來看幾個簡單的互動:
>irb --simple-prompt
>> 1 + 2
=> 3
>> _
=> 3
>> 1 + _
=> 4
>> _
=> 4
>> print "Hello! Ruby!\n"
Hello! Ruby!
=> nil
>>
>> 1 + 2
=> 3
>> _
=> 3
>> 1 + _
=> 4
>> _
=> 4
>> print "Hello! Ruby!\n"
Hello! Ruby!
=> nil
>>
這執行了1 + 2,顯示結果為3,_代表了互動環境中上一次運算結果,方便你在下一次的運算中直接取用上一次的運算結果。最後一次執行了print,這個方法可顯示指定的文字,print不會換行,所以字串最後加上\n表示要換行,最後=> nil表示print執行結束沒有傳回結果,這是蠻有用的資訊,如果真的不想看到,可以在執行irb時加上--noecho,不過這也就不會顯示如1+2的執行結果:
>irb --simple-prompt --noecho
>> print "Hello! Ruby!\n"
Hello! Ruby!
>> 1 + 2
>>
>> print "Hello! Ruby!\n"
Hello! Ruby!
>> 1 + 2
>>
再來看看其它的一些互動:
>> for i in 1..4
>> puts i
>> end
1
2
3
4
=> 1..4
>> def do_some()
>> puts "Hi"
>> end
=> nil
>> do_some()
Hi
=> nil
>> for
?>
?>
?> end
SyntaxError: (irb):30: syntax error, unexpected keyword_end
from C:/Winware/Ruby191/bin/irb:12:in `<main>'
>>
>> puts i
>> end
1
2
3
4
=> 1..4
>> def do_some()
>> puts "Hi"
>> end
=> nil
>> do_some()
Hi
=> nil
>> for
?>
?>
?> end
SyntaxError: (irb):30: syntax error, unexpected keyword_end
from C:/Winware/Ruby191/bin/irb:12:in `<main>'
>>
你可以在互動中直接觀察程式碼的執行結果,Ruby的許多定義都是以end結尾,如果在irb中輸入錯誤了,可以嘗試輸入end回到正常提示字元。
你可以撰寫一個純文字檔案,建議副檔名為.rb,在當中撰寫Ruby程式碼:
- hello.rb
puts "Hello! Ruby!"
程式中puts會將指定的文字輸出後換行,接著如下執行ruby指令啟動Ruby直譯器,載入指令稿直譯並執行:
>ruby hello.rb
Hello!Ruby!
>
Hello!Ruby!
>
如果只是要測試一小段簡單的指令稿,不一定要寫.rb檔案,也不一定要進入irb,可以在執行ruby指令時,於-e之後用單引號括住指令稿,多行程式碼時以分號區隔。例如:
>ruby hello.rb
Hello!Ruby!
>ruby -e 'print "Hello! Ruby!\n"; puts "Hello! Ruby"'
Hello! Ruby!
Hello! Ruby
>
Hello!Ruby!
>ruby -e 'print "Hello! Ruby!\n"; puts "Hello! Ruby"'
Hello! Ruby!
Hello! Ruby
>
在執行ruby指令時,可以指定-c僅檢查語法但不執行程式,指定-w顯示額外警訊,由於-c、-w經常一起指定,所以有個-cw可以達到分別指定-c、-w的效果。例如:
>ruby -c -w hello.rb
Syntax OK
>ruby -cw -e 'x = 10; print (1 + x)'
-e:1: warning: (...) interpreted as grouped expression
Syntax OK
>
Syntax OK
>ruby -cw -e 'x = 10; print (1 + x)'
-e:1: warning: (...) interpreted as grouped expression
Syntax OK
>
由於Ruby在呼叫方法時可以省略括號,上例第二個指令稿中,(1 + x)的括號被直譯器解釋為優先執行1 + x,而不會是print方法的括號,雖然就這個例子而言,哪個解釋的執行結果都相同,但有些情況下這類的解釋可能不是你想要的,於是直譯器提出了警告訊息。
執行ruby指令時,可以指定--version:
>ruby --version
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]
>
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]
>
指定-v時,會先顯示版本訊息,而後像警告模式-w執行程式:
>ruby -v
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]
Hello! Ruby!
>
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]
Hello! Ruby!
>
以上是常用的ruby指令選項,如果想知道更多選項,可以執行ruby時鍵入--help或-h顯示說明清單:
>ruby -h
Usage: ruby [switches] [--] [programfile] [arguments]
-0[octal] specify record separator (\0, if no argument)
-a autosplit mode with -n or -p (splits $_ into $F)
-c check syntax only
-Cdirectory cd to directory, before executing your script
-d set debugging flags (set $DEBUG to true)
-e 'command' one line of script. Several -e's allowed. Omit [programfile]
-Eex[:in] specify the default external and internal character encodings
-Fpattern split() pattern for autosplit (-a)
-i[extension] edit ARGV files in place (make backup if extension supplied)
-Idirectory specify $LOAD_PATH directory (may be used more than once)
-l enable line ending processing
-n assume 'while gets(); ... end' loop around your script
-p assume loop like -n but print line also like sed
-rlibrary require the library, before executing your script
-s enable some switch parsing for switches after script name
-S look for the script using PATH environment variable
-T[level] turn on tainting checks
-v print version number, then turn on verbose mode
-w turn warnings on for your script
-W[level] set warning level; 0=silence, 1=medium, 2=verbose (default for
level)
-x[directory] strip off text before #!ruby line and perhaps cd to directory
--copyright print the copyright
--version print the version
>
Usage: ruby [switches] [--] [programfile] [arguments]
-0[octal] specify record separator (\0, if no argument)
-a autosplit mode with -n or -p (splits $_ into $F)
-c check syntax only
-Cdirectory cd to directory, before executing your script
-d set debugging flags (set $DEBUG to true)
-e 'command' one line of script. Several -e's allowed. Omit [programfile]
-Eex[:in] specify the default external and internal character encodings
-Fpattern split() pattern for autosplit (-a)
-i[extension] edit ARGV files in place (make backup if extension supplied)
-Idirectory specify $LOAD_PATH directory (may be used more than once)
-l enable line ending processing
-n assume 'while gets(); ... end' loop around your script
-p assume loop like -n but print line also like sed
-rlibrary require the library, before executing your script
-s enable some switch parsing for switches after script name
-S look for the script using PATH environment variable
-T[level] turn on tainting checks
-v print version number, then turn on verbose mode
-w turn warnings on for your script
-W[level] set warning level; 0=silence, 1=medium, 2=verbose (default for
level)
-x[directory] strip off text before #!ruby line and perhaps cd to directory
--copyright print the copyright
--version print the version
>