This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$files # 全域變數 | |
@data # 實體(Instance)變數 | |
@@counter # 類別(Class)變數 | |
empty? # A Boolean-valued method or predicate | |
sort! # An in-place alternative to the regular sort method | |
timeout= # A method invoked by assignment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test = 1 | |
puts test.to_s | |
puts test.class | |
test = "xx" | |
puts test.to_s | |
puts test.class | |
#結果如下 | |
#ruby test.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ #全域(Global)變數 | |
@ #實體(Instance)變數 | |
[a-z] or _ #區域(Local)變數 | |
[A-Z] #常數(Constant)變數 | |
@@ #類別(Class)變數 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = 10 | |
=> 10 | |
defined? x | |
=> "local-variable" | |
$x = 10 | |
=> 10 | |
defined? $x | |
=> "global-variable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if windows | |
listcmd = 'dir' | |
else | |
listcmd = 'ls' | |
end | |
listing = `#{listcmd}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 5.times{ puts "test".object_id } | |
85641038 | |
85640870 | |
85640702 | |
85640534 | |
85638980 | |
=> 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
?\t # Character literal for the TAB character | |
?\C-x # Character literal for Ctrl-X | |
?\111 # Literal for character whose encoding is 0111 (octal) | |
x = ?\t | |
print x | |
=> 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = "hello " | |
a << "world" #=> "hello world" | |
a.concat(33) #=> "hello world!" | |
b = "hello " | |
b << "world" #=> "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = 'hello'; # Ruby 1.8 | |
s[0] # 104: the ASCII character code for the first character 'h' | |
s[s.length-1] # 111: the character code of the last character 'o' | |
s[-1] # 111: another way of accessing the last character | |
s[-2] # 108: the second-to-last character | |
s[-s.length] # 104: another way of accessing the first character | |
s[s.length] # nil: there is no character at that index | |
############################################################################ | |
s = 'hello'; # Ruby 1.9 | |
s[0] # 'h': the first character of the string, as a string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s[0] = ?H # 把第一個字元取代成 'H' | |
s[-1] = ?O # 把最後一個字元取代成 '0' |