Rails 1.2新特性体验(杂记)

1 Range#to_s(:db)
[code]>> (7.days.ago..1.day.ago).to_s(:db)
=> "BETWEEN '2006-12-11 02:06:50' AND '2006-12-17 02:06:50'" [/code]

2 Time Calculations
[code]>> Time.days_in_month(2)
=> 28
>> Time.now.seconds_since_midnight
=> 8709.840965

# last_year, next_year, last_month, next_month
>> Time.now.last_year
=> Sun Dec 18 02:25:59 -0800 2005
>> Time.now.next_month
=> Thu Jan 18 02:26:41 -0800 2007

# beginning_of_day, end_of_day, beginning_of_month, end_of_month
# beginning_of_quarter, beginning_of_year
>> Time.now.beginning_of_day
=> Mon Dec 18 00:00:00 -0800 2006

# yesterday, tomorrow, next_week(day = :monday)
>> Time.now.tomorrow
=> Tue Dec 19 02:28:01 -0800 2006
>> Time.now.next_week(:friday)
=> Fri Dec 29 00:00:00 -0800 2006

# valid symbol keys for #change:
# year, month, mday, hour, min, sec, usec
>> Time.now
=> Mon Dec 18 02:33:17 -0800 2006
>> Time.now.change(:hour => 1)
=> Mon Dec 18 01:00:00 -0800 2006

>> Time.now.in(5.days)
=> Sat Dec 23 02:34:59 -0800 2006[/code]

3 with_options
DRY你的路由配置。
[code]# map.article ':year/:month/:day/:permalink', :controller => 'mephisto', :action => 'show'
# map.article ':year/:month/:day/', :controller => 'mephisto', :action => 'daily'
# map.search 'search/:q', :controller => 'mephisto', :action => 'search', :q => nil
等同
# map.tags '*tags', :controller => 'mephisto', :action => 'list'
map.with_options :controller => 'mephisto' do |m|
m.article ':year/:month/:day/:permalink', :action => 'show'
m.article ':year/:month/:day/', :action => 'daily'
m.search 'search/:q',:action => 'search', :q => nil
m.tags '*tags', :action => 'list'
end [/code]

4 blank?
[code]>> 0.blank?
=> false
>> " ".blank?
=> true
>> [].blank?
=> true
>> {}.blank?
=> true
>> nil.blank?
=> true [/code]

5 returning
[code]def create_book
book = Book.new
book.title = "Trafalgar: The Nelson Touch"
book.author = "David Howarth"
@db.add(book)
book
end
等同
def create_book
returning Book.new do |book|
book.title = "Trafalgar: The Nelson Touch"
book.author = "David Howarth"
@db.add(book)
end
end
[/code]

6 Time#to_s(:format)
[code]>> Time.now.to_s(:time)
=> "01:50"
>> Time.now.to_s(:short)
=> "Dec 18, 2006"
>> Time.now.to_s(:db)
=> "2006-12-18 01:50:42"
>> time.to_s(:stamp)
=> 5:23PM
>> time.to_s(:relative)
=> today [/code]

7 cattr_reader, cattr_writer, and cattr_accessor
cattr_*与class_inheritable_*有比较的地方。可以参考前面所讲
[code] class MailTruck
cattr_accessor :trucks, :customers
end
MailTruck.trucks = [:UPS_TRUCK_9189, :UPS_TRUCK_9192]
>> class Parent; cattr_accessor :val; end
=> [:val]
>> class Child1 < Parent; end
=> nil
>> class Child2 < Parent; end
=> nil
>> Child1.val = 4
=> 4
>> Child2.val
=> 4
>> Child2.val = 5
=> 5
>> Child1.val
=> 5


>> class Parent; class_inheritable_accessor :val; end
=> [:val]
>> class Child1 < Parent; end
=> nil
>> class Child2 < Parent; end
=> nil
>> Child1.val = 4
=> 4
>> Child2.val = 4
=> 4
>> Child2.val = 5
=> 5
>> Child1.val
=> 4[/code]

8 delegate
不大清楚它的内涵。只能通过实例去猜了。那位那是知道。请回复。
[code]class Shipment < ActiveRecord::Base

def mailing_address
user ? user.mailing_address : nil
end

end

等同

class Shipment < ActiveRecord::Base

delegate :mailing_address, :to => :user

end[/code]

9 Symbol#to_proc
[code]>> %w[apple dell hp].map { |company| company.first }
=> ["a", "d", "h"]
>> %w[apple dell hp].map(&:first)
=> ["a", "d", "h"] [/code]

10 Proc#bind
[code] >> %w[joe tim francis].map &with_index { |x| [x, index] }
=> [["joe", 0], ["tim", 1], ["francis", 2]]

>> %w[badger goat mule eagle shark].select &with_index { even }
=> ["badger", "mule", "shark"][/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值