Ruby 學習日誌1 基礎

2021-04-12 21:16:34 字數 3545 閱讀 4709

鬱悶壞了, 寫了三個小時的ruby筆記居然在提交時丟失了.

只好重新寫一點兒還想得起的.

函式def say_goodnight(name)

result = "good night " + name

return result

end#time for bed...

puts say_goodnight("olojiang")

puts say_goodnight("yy")

執行ruby filename

或ruby

puts "hello world"

ctrl + d

riri -c

ri gc

ri gc::start

ri gc.start

ri --help

irbirb(main):001:0> "asdfl;kjsdfsdf".length

=> 14

irb(main):002:0> "risk".index("c")

=> nil

irb(main):003:0> "risk".index("s")

=> 2

irb(main):004:0> -192.abs

=> 192

irb(main):005:0> exit

#function

def say_goodnight(name)

result = "good night " + name

return result

enddef say_goodnight2(name)

"good night, #"

end#call method

puts

puts "************* call method"

puts say_goodnight("olojiang")

puts say_goodnight("yy")

puts(say_goodnight("jw"))

puts("and good night!/ngrandma")

#in a string #

puts

puts "************* in a string #"

puts(say_goodnight2("yjl"))

puts(say_goodnight2("jw"))

#variable

puts

puts "************* variable"

$greeting = "hello"

@name = "procedure"

puts("#$greeting #@name")

#array

puts

puts "************* array"

a = [1, "test string", 234.3]

puts a[2]

a[0] = nil

puts a

a = ['ant', 'cat']

puts a

a = %w[x y z]

puts a

#hash

puts

puts "************* hash"

hash_table =

puts hash_table["key1"]

puts hash_table["key2"]

puts hash_table[3][1]

hash_table2 = hash.new(999)

puts("default hash value: #")

#ifputs

puts "************* if"

count = 10

if count>0

count -=5

puts count

endcount -= 10 if count>= 5

puts count

#while

puts

puts "************* while"

count = 11

while(count>0)

count -=5

puts count

endnum = 2

num = num*num while num<1000

puts(num)

#regular expression

puts

puts "************* regular expression"

puts "hasdfsdf" =~ /f./

puts "hasdfsdf" =~ /df.*f/

line = "perl is good, python is nice"

puts(line.sub(/perl/, "ruby"))

puts(line.gsub(/perl|python/, "ruby"))

#block

puts

puts "************* block"

def greet

puts "start method"

yield("greet")

yield("greet")

puts "end method"

end

greet " }

puts

greet do

|method_name|

puts "multi line"

puts "block"

puts "method name is: #"

end#for each and block

puts

puts "************** for each and block"

['cat', 'dog', 'horse'].each

puts

5.times

puts

3.upto(6)

puts

('a'..'e').each

puts

#printf

puts

puts "************** printf"

printf("number: %5.2f /nstring: %s/n", 23.234, "hello")

#gets

puts

puts "************** gets"

print "input: "

line = gets

puts line

ruby學習隨筆1

函式的括號可有可無,如puts x 或puts x 為避免使用歧義,最好使用括號 在不影響函式使用的情況下可以去掉括號。語句末尾的分號可有可無,一行語句有多個語句時要用分號隔開。只有nil和false是假,其他都是真。puts直接輸出一行內容,包括回車換行 print輸出字串,但是不包含回車換行 p...

Ruby基礎學習(一)

ruby算是我們平常自動化測試中常用的指令碼語言了,最近開始學習ruby,將這些學習的點滴記錄下來。1.輸出 最常用的是puts和print。很明顯puts是帶有回車的,而print不包括回車的。2.注釋 在ruby語言中,注釋是比較接近shell指令碼的,單行注釋可以使用 從 開始到 結束。在上面...

Ruby學習系列 疑問 1

code def saygoodnight name result goodnight result goodnight,name return result end code code puts saygoodnight john bo code goodnight,john bo 執行成功 co...