Neeraj Kumar
2007-Jan-01 06:37 UTC
[:year, :month, :day].each { |m| delegate m, :to => :published_at }
Given below is a model from mephisto blogging tool. class Content < ActiveRecord::Base filtered_column :body, :excerpt belongs_to :user, :with_deleted => true belongs_to :site [:year, :month, :day].each { |m| delegate m, :to => :published_at } end I couldn''t understand the last line. Anyone care to explain what''s happening in the last line. Thanks. - Neeraj --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This is the quick way of writing the following: def year published_at.year end def year=(y) published_at.year = y end # Same for month and day The ''delegate'' method allows you to delegate a member access to a member with the same name on a different member object, so you can avoid writing ''content.published_at.year'' --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, Tom, that''s really handy. I was wondering where this ''delegate'' method is defined, so I looked it up and found it in ActiveSupport--it''s a method defined in the Module class. Pretty tricky :) Available here, if anyone else is interested: activesupport/lib/active_support/core_ext/module/delegation.rb Duane Johnson (canadaduane) http://blog.inquirylabs.com/ On Jan 1, 2007, at 12:52 AM, Tom Fakes wrote:> > This is the quick way of writing the following: > > def year > published_at.year > end > > def year=(y) > published_at.year = y > end > > # Same for month and day > > The ''delegate'' method allows you to delegate a member access to a > member with the same name on a different member object, so you can > avoid writing ''content.published_at.year'' > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---