class Invoice
def Invoice.generate(order_id, charge_amount, credited_amount = 0.0)
Invoice.new(:order_id => order_id, :amount => charge_amount, :invoice_type => PURCHASE, :credited_amount => credited_amount)
end
end
Why would you create Invoice.generate inside Invoice class rather than self.generate?
self.generateis easier to work with, whereasInvoice.generateis arguably more explicit. Other than that, there’s no difference between the two.Explanation
You can define a method on any instance using this form
Check this out
And yes, I mean any instance. It’s absolutely possible that one instance has some method while another doesn’t
A reminder: inside of class definition (but outside of method definitions)
selfpoints to that class.So, armed with this knowledge, the difference between
self.generateandInvoice.generateshould be obvious.