Agenda
- Loop
- Expression
- File Read/Write
- Debug
- Process & Thread
Loop
while
a = 10
while a > 0puts aa -= 1
end
until
a = 100until a == 0puts aa -= 1
end
loop
a = 10loop dobreak if a <= 0puts aa -= 1
end
循环控制
- break
- next
break
for x in 1..10break if x == 5puts x
end
next
for x in 1..10next if x == 6puts x
end
Expression
=~
正则匹配 匹配到返回首字符匹配的位置,匹配不到返回nil
/hello/i =~ "hello world" # => 0
!~
正则匹配 是否匹配不到 匹配到返回fals 匹配不到返回true
/666/i !~ "hello world" # => true
alias
别名
def hello'hello'
endalias old_hello hellodef hello'new hello'
endputs old_hello
puts hello
File Read/Write 文件读写
File.read
File.readlines
File#rewind etc
IO.read/write
File Read
file = File.open('run.log', 'r')
while line = file.getsputs line
end
File Write
file = File.open('run.log', 'a+')
file.puts 'hello'
file.close
File.open('run.log', 'a+') do |f|f.puts 'hello'
end
Debug
Debug 工具
- ruby debugger
- byebug
byebug
$ gem install byebug
require 'byebug'def hello namebyebug #此处会有断电,然后可以看上下文的变量puts name
endhello 'world'
Process & Thread 进程和线程
Process
pid = Process.fork{#...
}
Thread
Thread.new{#...
}