Hi I''d like to modify the behavior of the << method so that duplicates are not appended to my AR objects association. I thought I could just use alias_method_chain on it but I can''t seem to make the right method for it let alone make the call to alias_method_chain. I tried in the console.>> a = ""=> "">> def a.<<_with_extra( *args )puts "Test" end so that I could use alias_method_chain :<<, :extra How is this done? Cheers Daniel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-25 13:48 UTC
Re: alias_method_chain :<<
Hi -- On Sun, 26 Nov 2006, Daniel N wrote:> Hi > > I''d like to modify the behavior of the << method so that duplicates are not > appended to my AR objects association. > > I thought I could just use alias_method_chain on it but I can''t seem to make > the right method for it let alone make the call to alias_method_chain. > > I tried in the console. > >>> a = "" > => "" >>> def a.<<_with_extra( *args ) > puts "Test" > end > > so that I could use > alias_method_chain :<<, :extra > > How is this done?I think you''re going to have to use a word, like "append_extra". While << is a legitimate method name, <<_other_stuff is parsed as a here-document. Of course, you can smuggle in weird method names with define_method and send: class << a define_method("<<_with_extra_args") {|*args| ... } end a.send("<<_with_extra_args",x,y,z) but that of course is awkward and opaque. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can do it, but you''ve got to be careful because you cant simply write def <<_without_extra(*args) Ruby sees the "<<" as the beginning of a heredoc. Something like this should work, making sure method names starting with "<<" are defined with strings: class Array define_method "<<_with_extra" do |*args| # Do whatever you like here send("<<_without_extra") end alias_method_chain :<<, :extra end That should work as expected, but there may be a better way to stop duplicates being added to the association. If it is a has_many :through you could add uniqueness validation on the join model, or if it''s has_and_belongs_to_many you could use the :uniq option on the association. -Jonathan. On 11/26/06, Daniel N <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi > > I''d like to modify the behavior of the << method so that duplicates are > not appended to my AR objects association. > > I thought I could just use alias_method_chain on it but I can''t seem to > make the right method for it let alone make the call to alias_method_chain. > > I tried in the console. > > >> a = "" > => "" > >> def a.<<_with_extra( *args ) > puts "Test" > end > > so that I could use > alias_method_chain :<<, :extra > > How is this done? > > Cheers > Daniel > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 11/26/06, Jonathan Viney <jonathan.viney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You can do it, but you''ve got to be careful because you cant simply write > > def <<_without_extra(*args) > > Ruby sees the "<<" as the beginning of a heredoc. Something like this > should work, making sure method names starting with "<<" are defined with > strings: > > class Array > define_method "<<_with_extra" do |*args| > # Do whatever you like here > send("<<_without_extra") > end > > alias_method_chain :<<, :extra > end > > That should work as expected, but there may be a better way to stop > duplicates being added to the association. If it is a has_many :through you > could add uniqueness validation on the join model, or if it''s > has_and_belongs_to_many you could use the :uniq option on the association. > > -Jonathan.It is a has_many :through, but I didn''t realise that the :uniq option would prevent duplicates from being added. That''s all I''m really after. I guess I should realize by now that these kinds of things are already easy! Cheers --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---