Ruby 學習筆記
參考書--why’s (poignant) guide to ruby :CSDN資源ruby中的資料類型和其他基本操作資訊variables
any plain,lowercase word is a variable in ruby
例如 x,y,banana2,phone_a_quail
numbers
a series of digits which can start with a plus or minus sign
例如 1,23,-10000
strings
any sort of character surrounded by quotes
例如 "sealab" ,'2021'
symbols
words that look just like variables,start with a colon
例如 :a , :b :ponce_de_leon
constants
words like variables,but they are capitalized
例如 Time, Array,Bunny_Lake_is_Missing
methods
attached to the end od variables and constants by a dot
例如 front_door.open
front_door.is_open? 用來測試使用 Door_test_to_see_if_its_open
method arguments
attached to the end of a method, usually surrounded by parenthese and separated by commas
例如 front_door.paint(3, :red)
front_door.paint(3,:red).dry(30).close() 函數可以鏈式執行
class methods
usually attached after variables and constants, used a double colon
例如 Door::new(:oak)
global variables
variables which begin with a dollar sign
例如 $x, $1, $chunky
instance variables
variables which begin with an at symbol
例如 @x ,@y ,@only_the_chunk
class variables
variables which begin with double at symbols
例如 @@x, @@y,@@i_will
class variables give an attribute to many related objects in ruby
blocks
any code surrounded by curly braces
例如 2.times{print "yes. i am a student, but never agian"}
loop do
print "much better"
print "i am better"
end
block arguments
a set of variables surrounded by pipe characters and separated by commas
例如 |x|, |y| ,|up ,down|
使用方法 at the beginning of a block 例如{|x, y| x+y}
ranges
two values surrounded by parentheses and separated by an ellipsis
例如 (1..3) 表示從1到3的數,('a'..'z')表示小寫字母
if a third dot is used, the last value in the range is exclueded
例如(0...5) 表示從0到4
arrays
a list surrounded by square brackets and separated by commas
例如 {1,2,3} ,{'coat','mittens','snowboard'}
hashes
a dictionary surrounded by curly braces,use an arrow like an equals sign
例如 {'a'=> 'aardvarx','b'=>'badgar'}
regular expressions
a set of characters surrounded by slashes
例如 /ruby/,/[0-9]+/, /^\d{3}-\d{3}-\d{4}/
operators
這裡僅列出不熟悉的
** =~ !~===.....
<=>
全部見圖
keywords
這裡僅列出不熟悉的
alias BEGIN(與begin 不同) elsif END(與end不同) ensure module nil(應該與NULL差不多) rescue retry self yield
全部見圖