- Comprehensive Ruby Programming
- Jordan Hudgens
- 558字
- 2021-07-02 21:13:30
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 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 period and then the method name, as shown here:
Invoice.print_out

Now, if I try to call the instance method with the same syntax, it throws an error:

You will notice that 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 in 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:
Invoice.new.convert_to_pdf
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. Consider this 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 to be bad programming practice. Essentially, this is creating 15 new objects in the program.
Creating an instance variable and calling all the methods with it is a better practice:
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.
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應(yīng)用教程(第2版)
- Learning Cython Programming(Second Edition)
- Mastering JavaScript Object-Oriented Programming
- 深入實踐Spring Boot
- Learning SQLite for iOS
- Python 3破冰人工智能:從入門到實戰(zhàn)
- Hands-On Natural Language Processing with Python
- C語言程序設(shè)計實驗指導(dǎo) (第2版)
- Simulation for Data Science with R
- Java EE 7 with GlassFish 4 Application Server
- PHP 8從入門到精通(視頻教學(xué)版)
- 深度學(xué)習(xí)程序設(shè)計實戰(zhàn)
- 人人都能開發(fā)RPA機(jī)器人:UiPath從入門到實戰(zhàn)
- 軟技能2:軟件開發(fā)者職業(yè)生涯指南
- Visual FoxPro程序設(shè)計實驗教程