Rails 1.2新特性体验(杂记)

阅读更多
1 Range#to_s(:db)
>> (7.days.ago..1.day.ago).to_s(:db)
=> "BETWEEN '2006-12-11 02:06:50' AND '2006-12-17 02:06:50'" 


2 Time Calculations
>> 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


3 with_options
DRY你的路由配置。
# 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  


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


5 returning
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


6 Time#to_s(:format)
>> 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 


7 cattr_reader, cattr_writer, and cattr_accessor
cattr_*与class_inheritable_*有比较的地方。可以参考前面所讲
 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


8 delegate
不大清楚它的内涵。只能通过实例去猜了。那位那是知道。请回复。
class Shipment < ActiveRecord::Base
  …
  def mailing_address
    user ? user.mailing_address : nil
  end
  …
end

等同

class Shipment < ActiveRecord::Base
  …
  delegate :mailing_address, :to => :user
  …
end


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


10 Proc#bind
 >> %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"]




















你可能感兴趣的:(Rails,ActiveRecord,HP,Apple,EXT)