- Read Tutorial
- Watch Guide Video
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 since it's important to see the different behavior.
For right now you can ignore the class syntax and focus on the functionality, especially the way in which both method types are called.
I'm going to create a class and add two methods in 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 syntax is that I use the world
self, for class methods, and the name by itself for instance methods.
Now, when I want to call the class method, I'm able to declare the name of the class followed by a
. and then the method name, like so:
Now, if I try to call the instance method with the same syntax, it throws an error:
If you notice, the error message says that
convert_to_pdf is an undefined method. In order to run instance methods we need to create an instance of that class to run it.
I'll give you a hint of what we'll see in the OOP section and we'll instantiate a new
Invoice like this:
i = Invoice.new i.convert_to_pdf
When you run this method, it prints the message without an error.
Now, if I try to access my class method in the same manner that I called the instance method it will result an error, as shown here:
So, a class method can be called in conjunction with the name of the class whereas the instance method requires you to create an instance to call the method.
As a side note, I can shortcut my code a bit to call my instance method on the same line as the class name.
And this will work properly. However, typically it makes no sense to call a method this way because you're not going to be able to do much with it. So, the earlier way of accessing an instance method is better.
You may be wondering why you need an instance method at all when it's so much easier to call a class method.
Let's imagine that you have 15 methods in your class, you wouldn't want to call the class that many times in your program. For example:
class Invoice # 15 methods inside end Invoice.method_1 Invoice.method_2 Invoice.method_3 Invoice.method_4 Invoice.method_5 Invoice.method_6 Invoice.method_7 Invoice.method_8 Invoice.method_9 Invoice.method_10 Invoice.method_11 Invoice.method_12 Invoice.method_13 Invoice.method_14 Invoice.method_15
Calling the class every time doesn't look good and is considered bad programming practice. Essentially this is creating 15 new objects in the program.
It's a better practice to create an instance variable and call all the methods with it.
class Invoice # 15 methods inside end i = Invoice.new i.method_1 i.method_2 i.method_3 i.method_4 i.method_5 i.method_6 i.method_7 i.method_8 i.method_9 i.method_10 i.method_11 i.method_12 i.method_13 i.method_14 i.method_15
This way you're not creating a new instance of the class every time, instead you're using the same instance for all methods.
I hope this gives you an idea of the different types of methods available in Ruby and how to call them.