1. Ruby 中純數據結構 ( Struct 與 OpenStruct )
講一下它倆之間的區別:Struct 需要開頭明確聲明字段; 而 OpenStruct 人如其名, 隨時可以添加屬性
Struct 性能優秀; 而 OpenStruct 差點, 具體的性能差距可看這裏:http://stackoverflow.com/questions/1177594/ruby-struct-vs-openstruct
Struct 是 Ruby 解釋器內置, 用 C 實現;
OpenStruct 是 Ruby 標準庫, Ruby 實現
API 不同: Struct API 與 OpenStruct
2. 聯合索引 gem composite_primary_keys
3. Hash assert_valid_keys 白名單
4. 網頁服務器 pow
5. instance_of?, is_a? Class
6. respond_to? 和 send, class_eval, instance_methods/define_method
http://ruby-china.org/topics/3434
7. method_missing,一個 Ruby 程序員的夢中情人
def method_missing(method, *args)
if method.to_s =~ /(.*)_with_cent$/
attr_name = $1
if self.respond_to?(attr_name)
'%.2f' % (self.send(attr_name).to_f / 100.00)
else
super
end
end
end
def self.method_missing(method_sym, *arguments, &block)
if method_sym.to_s =~ /^n_(.*)$/
find_by_process_class($1)
else
super
end
end
def method_missing(meth, *args, &blk)
# 支持 suspending? ending? init? force?
if meth.to_s =~ /(.*)\?$/
return is_status?($1)
end
# 支持 stat_to_init stat_to_suspending stat_to_ending stat_to_force
if meth.to_s =~ /^stat_to_(.*)$/
return trans_stat_to($1)
end
# 不要丟了super
super
end
8. config.middleware 通過 rake -T 可以查看
9. block/block_given? # yield if block_given?
10. 監視Rails進程內存泄漏的技巧
https://ruby-china.org/topics/15853
http://blog.linjunhalida.com/blog/ruby-memory-leak-debug/
http://blog.linjunhalida.com/blog/librr-debug-cpu-usage-high/
gem
https://github.com/binarylogic/memorylogichttps://github.com/noahd1/oink
11. # Excel Generator
PROVINCE = %w{ 安徽 北京 福建 甘肅 廣東 廣西 貴州 海南 河北 河南 黑龍江 湖北
湖南 吉林 江蘇 江西 遼寧 內蒙古 寧夏 青海 山東 山西 陝西 上海
四川 天津 西藏 新疆 雲南 浙江 重慶 }
MONTH = 1.upto(12).to_a
def self.total_to_xls(year = '2012', opts = {})
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
months = MONTH
months = opts[:month].to_s.split(/,/) if opts[:month]
fixed_row = months.collect{ |m| m.to_s + '月' }.insert(0, '')
sheet1.row(0).concat(fixed_row)
row1 = ['']
(months.size - 1).times { row1 << ['用戶數', '金額', '訂單數'] }
sheet1.row(1).concat(row1.flatten!)
row = 2
sheet1.row(row).insert(0, '全國')
months.each_with_index do |m, i|
sheet1.row(row).insert(i*3 + 1, self.monthly_users_count(m))
sheet1.row(row).insert(i*3 + 2, self.monthly_amount(m))
sheet1.row(row).insert(i*3 + 3, self.monthly_orders_count(m))
end
PROVINCE.each do |province|
row += 1
sheet1.row(row).insert(0, province)
months.each_with_index do |m, i|
sheet1.row(row).insert(i*3 + 1, self.monthly_users_count_by_province(m, province))
sheet1.row(row).insert(i*3 + 2, self.monthly_amount_by_province(m, province))
sheet1.row(row).insert(i*3 + 3, self.monthly_orders_count_by_province(m, province))
end
end
path = "tmp/phone_recharge.xls"
book.write path
path
end
12. Object#tap
params.tap {|p| p[:foo] = 'bar' }
13. Array#bsearch #select, reject, find
bsearch要求搜索的數組是排序過的require 'benchmark'
data = (0..50_000_000)
Benchmark.bm do |x|
x.report(:find) { data.find {|number| number > 40_000_000 } }
x.report(:bsearch) { data.bsearch {|number| number > 40_000_000 } }
end
user system total real
find 3.020000 0.010000 3.030000 (3.028417)
bsearch 0.000000 0.000000 0.000000 (0.000006)
14. Enumerable#flat_map
```ruby
module CommentFinder
def self.find_for_users(user_ids)
users = User.where(id: user_ids)
user.posts.map do |post|
post.comments.map |comment|
comment.author.username
end
end
end
end
```
flatten
```ruby
module CommentFinder
def self.find_for_users(user_ids)
users = User.where(id: user_ids)
user.posts.map { |post|
post.comments.map { |comment|
comment.author.username
}.flatten
}.flatten
end
end
```
flat_map
```ruby
module CommentFinder
def self.find_for_users(user_ids)
users = User.where(id: user_ids)
user.posts.flat_map { |post|
post.comments.flat_map { |comment|
comment.author.username
}
}
end
end
```
15. Array.new with a Block
class Board
def board
@board ||= Array.new(8) { Array.new(8) { '0' } }
end
end
16. <=>
def fix_minutes
until (0...60).member? minutes
@hours -= 60 <=> minutes
@minutes += 60 * (60 <=> minutes)
end
@hours %= 24
self
end