What are the differences between class and instance methods in Ruby?

Even though we haven't covered classes in Ruby, I thought it would be good to show you the differences between class and instance methods in Ruby since it's important to see the different behaviors.

For now, you can ignore the class syntax and focus on the functionality, especially the way in which both the method types are called.

I'm going to create a class and add two methods into it, the first being a class method and the second an instance method:

class Invoice   # Class method   def self.print_out     "Printed out invoice"   end   # Instance method   def convert_to_pdf     "Converted to PDF"   end    end 

If you notice, the only difference in the syntax is that I used the self word ...

Get Comprehensive Ruby Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.