Hi, I''m going through the source code of RAM(Ruby asset manager) http://www.locusfoc.us/ram. I''m looking at the source code of a model Article.rb. It has bunch of instance methods and then this. class << self def search(query, groups, order = nil) groups = [groups].flatten find(:all, :select => "articles.*", :joins => "INNER JOIN linkings ON articles.id = linkings.linkable_id", :conditions => ["linkings.group_id IN (#{groups.join('','')}) AND (linkings.linkable_type=''Article'') AND ( articles.title LIKE ? OR articles.body LIKE ? OR (SELECT tags.name FROM tags INNER JOIN taggings ON tags.id = taggings.tag_id WHERE taggings.taggable_idarticles.id AND taggings.taggable_type = ''Article'' AND tags.name LIKE ?) IS NOT NULL)", "%#{query}%", "%#{query}%", "%#{query}%"], :group => " articles.id", :order => order) end end My question is why go all the way and do class << stuff to get a class method. What''s wrong with using just def self. search(query, groups, order = nil) ..... end What are the design benefits. I have read ruby book and can understand in some cases it''s useful. But in this case when we have a simple model why not just have self.method_name. Thanks. - --~--~---------~--~----~------------~-------~--~----~ 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/28/06, Neeraj Kumar <neeraj.jsr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m going through the source code of RAM(Ruby asset manager) > http://www.locusfoc.us/ram. > > I''m looking at the source code of a model Article.rb. > > It has bunch of instance methods and then this. > > > > class << self > def search(query, groups, order = nil) > groups = [groups].flatten > find(:all, :select => "articles.*", :joins => "INNER JOIN linkings ON > articles.id = linkings.linkable_id", :conditions => ["linkings.group_id IN > (#{groups.join('','')}) AND > (linkings.linkable_type=''Article'') AND (articles.title LIKE > ? OR articles.body LIKE ? OR (SELECT tags.name FROM tags INNER JOIN taggings > ON tags.id = taggings.tag_id WHERE taggings.taggable_id = articles.id AND > taggings.taggable_type = ''Article'' AND tags.name LIKE ?) IS NOT NULL)", > "%#{query}%", "%#{query}%", "%#{query}%"], :group => " articles.id", :order > => order) > end > end > > > My question is why go all the way and do class << stuff to get a class > method. What''s wrong with using just > def self. search(query, groups, order = nil) > ..... > end > > What are the design benefits. I have read ruby book and can understand in > some cases it''s useful. But in this case when we have a simple model why not > just have self.method_name. > > Thanks.Hi, This should be helpful: http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html and http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html - Rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-28 21:05 UTC
Re: What''s the deal with class << self
Hi -- On Tue, 28 Nov 2006, Rob Sanheim wrote:> > On 11/28/06, Neeraj Kumar <neeraj.jsr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, >> >> I''m going through the source code of RAM(Ruby asset manager) >> http://www.locusfoc.us/ram. >> >> I''m looking at the source code of a model Article.rb. >> >> It has bunch of instance methods and then this. >> >> >> >> class << self >> def search(query, groups, order = nil) >> groups = [groups].flatten >> find(:all, :select => "articles.*", :joins => "INNER JOIN linkings ON >> articles.id = linkings.linkable_id", :conditions => ["linkings.group_id IN >> (#{groups.join('','')}) AND >> (linkings.linkable_type=''Article'') AND (articles.title LIKE >> ? OR articles.body LIKE ? OR (SELECT tags.name FROM tags INNER JOIN taggings >> ON tags.id = taggings.tag_id WHERE taggings.taggable_id = articles.id AND >> taggings.taggable_type = ''Article'' AND tags.name LIKE ?) IS NOT NULL)", >> "%#{query}%", "%#{query}%", "%#{query}%"], :group => " articles.id", :order >> => order) >> end >> end >> >> >> My question is why go all the way and do class << stuff to get a class >> method. What''s wrong with using just >> def self. search(query, groups, order = nil) >> ..... >> end >> >> What are the design benefits. I have read ruby book and can understand in >> some cases it''s useful. But in this case when we have a simple model why not >> just have self.method_name. >> >> Thanks. > > Hi, > > This should be helpful: > > http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html > and > http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.htmlI''ll throw my hat in the ring :-) http://www.rubypal.com/singletons.html (an essay from last year that just got resuscitated today amidst some singleton-class discussion on IRC) Mind you, Neeraj was mainly asking about the relative merits of the two techniques, rather than the underlying stuff. To which I would say: the class << obj technique is probably chosen mostly because it sort of "normalizes" the appearance of the code, so that every method definition is based on a bareword. On the other hand, the other way gives you an explicit visual reminder, with each method, that the method is being added to an object on a singleton basis. 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 -~----------~----~----~----~------~----~------~--~---