Skip to content

Instantly share code, notes, and snippets.

View EragonJ's full-sized avatar
🇯🇵
Learning Japanese

Ryu | Chia-Lung Chen EragonJ

🇯🇵
Learning Japanese
View GitHub Profile
$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
test = 1
puts test.to_s
puts test.class
test = "xx"
puts test.to_s
puts test.class
#結果如下
#ruby test.rb
$ #全域(Global)變數
@ #實體(Instance)變數
[a-z] or _ #區域(Local)變數
[A-Z] #常數(Constant)變數
@@ #類別(Class)變數
x = 10
=> 10
defined? x
=> "local-variable"
$x = 10
=> 10
defined? $x
=> "global-variable"
if windows
listcmd = 'dir'
else
listcmd = 'ls'
end
listing = `#{listcmd}`
>> 5.times{ puts "test".object_id }
85641038
85640870
85640702
85640534
85638980
=> 5
?\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
a = "hello "
a << "world" #=> "hello world"
a.concat(33) #=> "hello world!"
b = "hello "
b << "world" #=> "hello world"
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
s[0] = ?H # 把第一個字元取代成 'H'
s[-1] = ?O # 把最後一個字元取代成 '0'