I am extending my model with some methods. My question is when to and when not to define the method with self. example def sales_value ... all kinds of calculations end OR def self.sales_value ... all kinds of calculations end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The shortest (but not the most correct one) way to describe the difference is to say that def meth1; end is an object method while def self.meth2; end is a class method, and you cal them differently: a = Foo.new a.meth1 Foo.meth2 But there''s more than that As for Active Records it depends whether you want a method for an ActiveRecord instance or for the class Btw these are Ruby basics, find a good Ruby book/tutorial. Cheers, Yury On 5/11/07, bitterbal <portie-kFg6fFbE636XDw4h08c5KA@public.gmane.org> wrote:> > I am extending my model with some methods. My question is when to and > when not to define the method with self. > > example > > def sales_value > ... all kinds of calculations > end > > OR > > def self.sales_value > ... all kinds of calculations > end > > > > >-- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
bitterbal wrote:> I am extending my model with some methods. My question is when to and > when not to define the method with self. > > example > > def sales_value > ... all kinds of calculations > end > > OR > > def self.sales_value > ... all kinds of calculations > endMy answer is the same as Yuri''s, just different words. If sales_value effects or returns a different value for each row in the table don''t use self. You would then do this for example sale = Sale.find(id) puts "The sales value of sale #{id} is: #{sale}" If sales_value combines data from some or all rows in the table use self.sales_value. You might then do this: sales = Sale.sales_value #note there is no find here puts "the value of all sales is #{sales}" -- 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 -~----------~----~----~----~------~----~------~--~---
Maybe this will help you. When you are writing model code you are writing a class. That is "working class" not "learnstitute class." Think of "blue collar" and "white collar" workers. People of the "blue collar" class work a certain way that is difference from a "white collar" worker. So you may have people (instances) of "blue collar" and "white" collar workers. So you have a Class and some Objects of each class. Both the Class and the Objects can have methods (referred to as class and instance methods respectively). When you are writing your code you are writing a class represented in the def as "self." So..... def self.my_class_method # Can be called on the Class or instances of the class end def my_instance_method # Is only available to instances of the class end Calling them..... BlueCollarWorker.my_class_method # called on the class a_person_object.my_instance_method #called on the instance. On May 11, 6:18 pm, John Miller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> bitterbal wrote: > > I am extending my model with some methods. My question is when to and > > when not to define the method with self. > > > example > > > def sales_value > > ... all kinds of calculations > > end > > > OR > > > def self.sales_value > > ... all kinds of calculations > > end > > My answer is the same as Yuri''s, just different words. > > If sales_value effects or returns a different value for each row in the > table don''t use self. You would then do this for example > > sale = Sale.find(id) > puts "The sales value of sale #{id} is: #{sale}" > > If sales_value combines data from some or all rows in the table use > self.sales_value. You might then do this: > > sales = Sale.sales_value #note there is no find here > puts "the value of all sales is #{sales}" > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
> I am extending my model with some methods. My question is when to> and when not to define the method with self. Ruby 101 : class String def shuffle split(//u).sort_by { rand }.join('''') end CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a def self.random( len ) newpass = "" 1.upto(len) { |i| newpass << CHARS[rand(CHARS.size-1)] } return newpass end end puts String.random(10) puts "abcdef".shuffle Alain Ravet -------- http://blog.ravet.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 -~----------~----~----~----~------~----~------~--~---
Thanks. great example that makes it clear for me. On May 12, 12:18 am, John Miller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> bitterbal wrote: > > I am extending my model with some methods. My question is when to and > > when not to define the method with self. > > > example > > > def sales_value > > ... all kinds of calculations > > end > > > OR > > > def self.sales_value > > ... all kinds of calculations > > end > > My answer is the same as Yuri''s, just different words. > > If sales_value effects or returns a different value for each row in the > table don''t use self. You would then do this for example > > sale = Sale.find(id) > puts "The sales value of sale #{id} is: #{sale}" > > If sales_value combines data from some or all rows in the table use > self.sales_value. You might then do this: > > sales = Sale.sales_value #note there is no find here > puts "the value of all sales is #{sales}" > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---