7 月的 789 三天去北京参加了助教的回训活动,收获很多。
Xdite 老师讲的偏向「道」的层面,常有「提壶灌顶」之效力。
ihower 老师所讲偏向「术」的层面,给人「庖丁解牛」之感受。
答案是: clone + <CMD + Shift + F>
index
O(N)
vs O(log(N))
rails 中创建表时添加
index
add_index
def change
create_table :cities do |t|
t.string :province
t.string :city
t.timestamps
end
add_index :cities, :juhe_id
end
index
def change
create_table :trains do |t|
t.string :number, :index => true
t.timestamps
end
end
define_method
有没有问过,为什么可以这样用:
find
find_by_id
find_by_email
find_by_name
....
the Law of Demeter
先简单解释一下,这个 the Law of Demeter
()
有时用简单的比喻「别跟陌生人说话」,更贴切地说法是「别探朋友的隐私」,或「只同你最亲密的朋友交谈」。
或更粗暴的说法是「每行只用一个 dot」(这当然不是绝对的),但有借鉴意义。
像下面这种情况就要尽量避免:
在当前 model 中调用别的 model 的方法
class Invoice < ActiveRecord::Base
belongs_to :user
end
view
<%= @invoice.user.name %>
<%= @invoice.user.address %>
<%= @invoice.user.cellphone %>
class Invoice < ActiveRecord::Base
belongs_to :user
delegate :name, :address, :cellphone, :to => :user,
:prefix => true,
:allow_nil => true
end
view
<%= @invoice.user_name %>
<%= @invoice.user_address %>
<%= @invoice.user_cellphone %>
Factory Method
class InvoiceController < ApplicationController
def create
@invoice = Invoice.new(params[:invoice])
@invoice.address = current_user.address
@invoice.phone = current_user.phone
@invoice.vip = ( @invoice.amount > 1000 )
if Time.now.day > 15
@invoice.delivery_time = Time.now + 2.month
else
@invoice.delivery_time = Time.now + 1.month
end
@invoice.save
end
end
Model
class Invoice < ActiveRecord::Base
def self.new_by_user(params, user)
invoice = self.new(params)
invoice.address = user.address
invoice.phone = user.phone
invoice.vip = ( invoice.amount > 1000 )
if Time.now.day > 15
invoice.delivery_time = Time.now + 2.month
else
invoice.delivery_time = Time.now + 1.month
end
return invoice
end
end
Controller
class InvoiceController < ApplicationController
def create
@invoice = Invoice.new_by_user(params[:invoice], current_user)
@invoice.save
end
end
劣就是夯 (Worse is better.)
PHP / Javascript
注解之所在,重构之所在
代码面前,了无秘密
降维攻击的另一种直白的说法:到一大群傻叉当中
竞争是给弱者的(背后的意思是:成为强者,变成垄断者,就没有人与你竞争了)
the Law of Demeter
ruby programming
Growth Hack
Ohter Links