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
Collectioneach 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 utimerwa[+] 用a或者a+即可
Dirchdir entries("dir") foreach("dir")
[] glob
I/Oreadline 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。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值