|
锁定老贴子 主题: Rails 1.2新特性体验(杂记)
该帖已经被评为良好帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-01-08
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"]
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2006-12-22
好贴,mephisto的代码看过了,写得不错
|
|
| 返回顶楼 | |
|
最后更新时间:2006-12-22
melin 写道 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这个比较鬼……是不是必须有Shipment belongs_to User? |
|
| 返回顶楼 | |
|
最后更新时间:2006-12-27
应该是有,看样子,好像是根据自己的user方法找到user的吧。。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-01-18
8仅指把一个method delegate给另外一个对象实现。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-01-19
[quote]
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 [quote] 我想应该不用“再用belongs_to了吧” 那个“:to => :user” 就表示用user这个model了 “:mailing_address”这个是user里的字段或者方法, 用了”delegate :mailing_address“表示 Shipment这个model里有个叫mailing_address的字段或方法,然后这个“mailing_address”就是来自user的 所谓delegate(代表) 上面只是我看了上面的帖子猜测的,不知道对不对,有待于求证! |
|
| 返回顶楼 | |
|
最后更新时间:2007-01-20
## Model class Content < ActiveRecord::Base # the :to options tells which object # to pull the delegated method from delegate :year, :to => :published_at delegate :month, :to => :published_at delegate :day, :to => :published_at end # Which allows one do write Content.year # as a shortcut for Content.published_at.year gigix 写道 melin 写道 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这个比较鬼……是不是必须有Shipment belongs_to User? |
|
| 返回顶楼 | |
|
最后更新时间:2007-01-19
代码没有仔细看。。 不过mephisto的 admin界面太莫名了
baizheng 写道 好贴,mephisto的代码看过了,写得不错
|
|
| 返回顶楼 | |
|
最后更新时间:2007-01-20
xangd正解
|
|
| 返回顶楼 | |
|
最后更新时间:2007-01-23
引用 melin 写道
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 这个比较鬼……是不是必须有Shipment belongs_to User? 我想应该不用“再用belongs_to了吧” 那个“:to => :user” 就表示用user这个model了 “:mailing_address”这个是user里的字段或者方法, 用了”delegate :mailing_address“表示 Shipment这个model里有个叫mailing_address的字段或方法,然后这个“mailing_address”就是来自user的 所谓delegate(代表) 上面只是我看了上面的帖子猜测的,不知道对不对,有待于求证! |
|
| 返回顶楼 | |














