A method that belongs to instances of a class rather than the class itself is known as an instance method. Simply leave static out of the method heading to define an instance method. You refer to variables and methods in the class by their names, without a dot, in the method specification.
A class in Ruby is an entity that serves as a template for the creation of other objects. The methods available on any instance of a class are defined by the class. When a method is defined inside a class, it becomes an instance method on that class. That method will be available in any future instance of that class.
The instance method executes a series of operations on the data/value provided by instance variables. When we use instance variables in a method, we call it an instance method. A class method is a method that is called on the class as a whole, rather than on a specific instance of an object.
Class Objects: Declare a class object in the main method or from outside the class.
Method Definition: create the header and body code for the method as shown below.
Method Call: Call objectName.methodName() whenever you wish to use the method.
Share a personalized message with your friends.