用户形象图片

 

1 Range#to_s(:db)

代码
  1. >> (7.days.ago..1.day.ago).to_s(:db)   
  2. => "BETWEEN '2006-12-11 02:06:50' AND '2006-12-17 02:06:50'"   

2 Time Calculations

代码
  1. >> Time.days_in_month(2)   
  2. => 28  
  3. >> Time.now.seconds_since_midnight   
  4. => 8709.840965  
  5.   
  6. # last_year, next_year, last_month, next_month   
  7. >> Time.now.last_year   
  8. => Sun Dec 18 02:25:59 -0800 2005  
  9. >> Time.now.next_month   
  10. => Thu Jan 18 02:26:41 -0800 2007  
  11.   
  12. # beginning_of_day, end_of_day, beginning_of_month, end_of_month   
  13. # beginning_of_quarter, beginning_of_year   
  14. >> Time.now.beginning_of_day   
  15. => Mon Dec 18 00:00:00 -0800 2006  
  16.   
  17. # yesterday, tomorrow, next_week(day = :monday)   
  18. >> Time.now.tomorrow   
  19. => Tue Dec 19 02:28:01 -0800 2006  
  20. >> Time.now.next_week(:friday)   
  21. => Fri Dec 29 00:00:00 -0800 2006  
  22.   
  23. # valid symbol keys for #change:   
  24. #   year, month, mday, hour, min, sec, usec   
  25. >> Time.now   
  26. => Mon Dec 18 02:33:17 -0800 2006  
  27. >> Time.now.change(:hour => 1)   
  28. => Mon Dec 18 01:00:00 -0800 2006  
  29.   
  30. >> Time.now.in(5.days)   
  31. => Sat Dec 23 02:34:59 -0800 2006  

3 with_options
DRY你的路由配置。

代码
  1. # map.article ':year/:month/:day/:permalink', :controller => 'mephisto', :action => 'show'   
  2. # map.article ':year/:month/:day/', :controller => 'mephisto', :action => 'daily'   
  3. # map.search 'search/:q', :controller => 'mephisto', :action => 'search', :q => nil   
  4. 等同   
  5. # map.tags '*tags', :controller => 'mephisto', :action => 'list'   
  6. map.with_options :controller => 'mephisto' do |m|   
  7.   m.article ':year/:month/:day/:permalink',  :action => 'show'   
  8.   m.article ':year/:month/:day/', :action => 'daily'   
  9.   m.search  'search/:q',:action => 'search', :q => nil   
  10.   m.tags    '*tags', :action => 'list'   
  11. end    

4 blank?

代码
  1. >> 0.blank?   
  2. => false  
  3. >> " ".blank?   
  4. => true  
  5. >> [].blank?   
  6. => true  
  7. >> {}.blank?   
  8. => true  
  9. >> nil.blank?   
  10. => true    

5 returning

代码
  1. def create_book   
  2.   book = Book.new  
  3.   book.title = "Trafalgar: The Nelson Touch"  
  4.   book.author = "David Howarth"  
  5.   @db.add(book)   
  6.   book   
  7. end   
  8. 等同   
  9. def create_book   
  10.   returning Book.new do |book|   
  11.     book.title = "Trafalgar: The Nelson Touch"  
  12.     book.author = "David Howarth"  
  13.     @db.add(book)   
  14.   end   
  15. end   

6 Time#to_s(:format)

代码
  1. >> Time.now.to_s(:time)   
  2. => "01:50"    
  3. >> Time.now.to_s(:short)   
  4. => "Dec 18, 2006"    
  5. >> Time.now.to_s(:db)   
  6. => "2006-12-18 01:50:42"    
  7. >> time.to_s(:stamp)   
  8. => 5:23PM   
  9. >> time.to_s(:relative)   
  10. => today   

7 cattr_reader, cattr_writer, and cattr_accessor
cattr_*与class_inheritable_*有比较的地方。可以参考前面所讲

代码
  1.  class MailTruck   
  2.    cattr_accessor :trucks, :customers   
  3.  end   
  4.  MailTruck.trucks = [:UPS_TRUCK_9189, :UPS_TRUCK_9192]   
  5. >> class Parent; cattr_accessor :val; end   
  6. => [:val]   
  7. >> class Child1 < Parent; end   
  8. => nil   
  9. >> class Child2 < Parent; end   
  10. => nil   
  11. >> Child1.val = 4  
  12. => 4  
  13. >> Child2.val   
  14. => 4  
  15. >> Child2.val = 5  
  16. => 5  
  17. >> Child1.val   
  18. => 5  
  19.   
  20. >> class Parent; class_inheritable_accessor :val; end   
  21. => [:val]   
  22. >> class Child1 < Parent; end   
  23. => nil   
  24. >> class Child2 < Parent; end   
  25. => nil   
  26. >> Child1.val = 4  
  27. => 4  
  28. >> Child2.val = 4  
  29. => 4  
  30. >> Child2.val = 5  
  31. => 5  
  32. >> Child1.val   
  33. => 4  

8 delegate
不大清楚它的内涵。只能通过实例去猜了。那位那是知道。请回复。

代码
  1. class Shipment < ActiveRecord::Base   
  2.   …   
  3.   def mailing_address   
  4.     user ? user.mailing_address : nil   
  5.   end   
  6.   …   
  7. end   
  8.   
  9. 等同   
  10.   
  11. class Shipment < ActiveRecord::Base   
  12.   …   
  13.   delegate :mailing_address, :to => :user   
  14.   …   
  15. end  

9 Symbol#to_proc

代码
  1. >> %w[apple dell hp].map { |company| company.first }   
  2. => ["a""d""h"]   
  3. >> %w[apple dell hp].map(&:first)   
  4. => ["a""d""h"]   

10 Proc#bind

代码
  1. >> %w[joe tim francis].map &with_index { |x| [x, index] }   
  2. => [["joe"0], ["tim"1], ["francis"2]]   
  3.   
  4. >> %w[badger goat mule eagle shark].select &with_index { even }   
  5. => ["badger""mule""shark"]  

回到帖子顶部