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
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
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 [email protected] wrote:
OR
def self.sales_value
… all kinds of calculations
end
–
Best regards,
Yuri L.
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
endOR
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}”
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
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
end
def my_instance_method
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 M. [email protected]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs