ruby 笔记
- ruby的核心
- ruby的platform
- ruby的表达式 操作符和控制流
-
ruby的platform
范畴 | api | 注释 |
String |
[] index sub gsub match |
参数中可直接使用regexp |
split partition chomp strip | ||
Regexp | =~ split sub gsub | |
$~ $1 $& [] | 线程和method安全 | |
Time Date |
Time.now Time.local(2007, 7, 8) t.strftime("%Y-%m-%d %H:%M:%S") |
也可以utc |
require 'parsedate'; include ParseDate | ||
Collection | each each_with_index | |
collect find select(find_all) reject inject |
inject起到了reduce一个array的作用 | |
sort sort_by group_by(key相同的元素组成一个array) | ||
include? grep | ||
key? value? merge(update) invert inspect | ||
File | basename dirname split | |
exist? directory? stat | ||
open delete utime | rwa[+] 用a或者a+即可 | |
Dir | chdir entries("dir") foreach("dir") | |
[] glob | ||
I/O | readline each lineno read | |
puts printf write | ||
networking |
s = TCPSocket.open(host, port) s.gets s.close |
require 'socket' |
server = TCPServer.open(port) client = server.accept client .puts client.close
|
server = TCPServer.open(port) sockets = [server] ready = select(sockets) readable = ready[0] readable.each {|socket|} |
|
require 'net/http' http = Net::HTTP.new(host) headers, body = http.get(path)
|
||
11. Threads and Processes
ruby的线程 建立在ruby interpreter中 ,同os的线程没关系。这一点类似java虚拟机,就是一个进程里面跑着n个线程。
A. 创建线程
Thread.new(*args) {|args| block}
threads = [] for page_to_fetch in pages threads << Thread.new(page_to_fetch) do |url| puts "share: " + page_to_fetch + ' ' + url end end threads.each {|thr| thr.join }
除了block内的local变量 ,所有线程共享全局、实例和local变量。
B. 线程操控
Thread.join, Thread.list
Thread.current[]= 可保存block内变量,并被其他线程访问,使用方法类似hash。
上一篇: 手写实现call,apply,bind
下一篇: Rails 2.2 RC1发布