Hi all. How can I reflect on a method which had been extended into an association. If i try model.association.method(:method_name) I get an undefined error yet I can invoke it via model.association.method_name. I''ve tried several different approaches to get at the method but can''t seem to be able to get hold of it. Thanks Garrett --~--~---------~--~----~------------~-------~--~----~ 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 22 Oct 2007, at 15:37, garrett wrote:> > Hi all. > > How can I reflect on a method which had been extended into an > association. > > If i try model.association.method(:method_name) I get an undefined > error yet I can invoke it via model.association.method_name. I''ve > tried several different approaches to get at the method but can''t seem > to be able to get hold of it. >class Post has_many :comments do def some_method end end end The basic issues is that post.comments is not an array, it''s an instance of a proxy class that will (when needed) load the collection and forward methods. so when you call post.comments.method(:some_method) that''s being forwarded on to the array itself. You can use post.comments.proxy_respond_to?(:foo) to find if the proxy itself response to a method, but the ''method'' method has been undef''d on association proxies. Fred. --~--~---------~--~----~------------~-------~--~----~ 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 Fred Is there no way then for me to check that two method names actually have the same implementation. I had been intending the test that class Post has_many :comments do def foo end alias_method :bar, :foo end end post.comments.method(:foo) == post.comments.method(:bar) On Oct 22, 4:30 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 Oct 2007, at 15:37, garrett wrote: > > > > > Hi all. > > > How can I reflect on a method which had been extended into an > > association. > > > If i try model.association.method(:method_name) I get an undefined > > error yet I can invoke it via model.association.method_name. I''ve > > tried several different approaches to get at the method but can''t seem > > to be able to get hold of it. > > class Post > has_many :comments do > def some_method > end > end > end > > The basic issues is that post.comments is not an array, it''s an > instance of a proxy class that will (when needed) load the collection > and forward methods. > so when you call post.comments.method(:some_method) that''s being > forwarded on to the array itself. > > You can use post.comments.proxy_respond_to?(:foo) to find if the > proxy itself response to a method, but the ''method'' method has been > undef''d on association proxies. > > Fred.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
garrett wrote:> Thanks Fred > > Is there no way then for me to check that two method names actually > have the same implementation. > > I had been intending the test that > > class Post > has_many :comments do > def foo > end > alias_method :bar, :foo > end > end > > post.comments.method(:foo) == post.comments.method(:bar)The #method() method in Ruby isn''t very helpful for comparing implementations:>> "foo".method(:length) == "foo".method(:length)=> false If you need to check to see if an extension has been included in an association, you could grope through the ancestor modules. Or it may be easier to define a duck-type predicate in the extension module, like #is_my_fancy_type?, that you can use to check if the association has that type. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Josh Susser wrote:> garrett wrote: >> Thanks Fred >> >> Is there no way then for me to check that two method names actually >> have the same implementation. >> >> I had been intending the test that >> >> class Post >> has_many :comments do >> def foo >> end >> alias_method :bar, :foo >> end >> end >> >> post.comments.method(:foo) == post.comments.method(:bar) > > The #method() method in Ruby isn''t very helpful for comparing > implementations: > >>> "foo".method(:length) == "foo".method(:length) > => false > > If you need to check to see if an extension has been included in an > association, you could grope through the ancestor modules. Or it may be > easier to define a duck-type predicate in the extension module, like > #is_my_fancy_type?, that you can use to check if the association has > that type.Ah, oops. Ruby''s per-object methods get me again. Method identity is unique per instance. The two "foo" strings in my other example are of course different objects. Sigh. Symbols are the same instance though, therefore:>> :foo.method(:to_s) == :foo.method(:to_s)=> true So if you have the exact same association instance, you could actually compare implementations of messages to see if they were the same method. I still don''t get the use case though. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---