闭包
use block
1 | def foo |
use &
and call
1 | def foo(&block) |
proc
proc
可以看做是block
的对象表示,类名为Proc
1
proc = Proc.new {|x| x*2}
lambda
lambda的class也是Proc
1
lambda = lambda {|x| x*2}
lambda和proc的区别是: 实例化方式的不同而已,proc
更像对象,lambda
更像方法
1 | ## diff1 |
Class
基础类示例
1 | class Point |
继承
public
- visibility:
in/out
- inheritance:
Yes
- call an obj:
Yes
- visibility:
protected
- visibility:
within
- inheritance:
Yes
- call an obj:
Yes
- visibility:
private
- visibility:
within
- inheritance:
Yes
- call an obj:
No
- visibility:
1 | class Point3D < Point |
module
- include 引入成实例方法
- extend 引入成类方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18module Helper
# instance method
def test
end
# 类方法
module ClassMethods
def test2
end
end
# hook klass为引入的类这里是 Point
def self.included(klass)
klass.extend ClassMethods
end
end
# Point class
class Point
include Helper
end
方法优先级:singleton中的方法
- 实例方法
- module方法
- 父类实例方法
异常处理
产生: raise
处理: rescue1
2
3
4
5
6
7
8def foo
begin
# raise "boom in foo"
raise TypeError, "boom in foo", caller
rescue => e
puts e
end
end
枚举和比较模块
Comparable1
2
3
4
5
6
7
8
9
10class People
attr_reader :name
include Comparable
def initialize name
@name = name
end
def <=> other
self.name <=> other.name
end
end
Enumerable1
2
3
4
5
6
7
8
9
10
11
12
13class People
attr_reader :people
include Enumerable
def initialize people
@people = people
end
def each
raise "please provide a block" unless block_given?
people.each do |person|
yield person
end
end
end
正则表达式
生成正则的方法
- /ruby/
- %r{ruby}
- Regexp.new
使用1
2
3"ruby" =~ /ruby/ # 0
"ruby".match(/ru/) # ru
"ruby".gsub!(/r/, i) # iuby
正则编写测试网站1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30[abc] A single character of: a, b, or c
[^abc] Any single character except: a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary
(...) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
options:
i case insensitive
m make dot match newlines
x ignore whitespace in regex
o perform #{...} substitutions only once
时间、日期
- DateTime < Date
- Time # core library
1
2
3
4# 设置环境变量time zone
ENV['TZ'] = 'Asia/Shanghai'
# 或者
Time.now.new_offset(Rational(8,24))
文件操作
多线程
示例
1 | def foo |
Thread.new 创建线程
t1.priority = -1
t1.priority = 1 # 设置优先
t1.join 开始执行t1线程,主线程等待t1完成
Thread.current # 当前线程对象
Thread.abort_on_exception = true # 一个线程异常,所有线程都退出
mutex
1 | mutex = Mutex.new |
元编程(metaprogramming)
eval
1 | eval "1 + 1" |
instance_eval
1 | String.instance_eval "def foo; puts 'instance_eval foo'; end" |
class_eval
1 | String.class_eval "def foo; puts 'class_eval foo'; end" |
define_method
1 | define_method(:foo) { puts 'abc' } |
1 | def method_missing method_name |