Defining Method
We must first define a method before we can utilize it. The def keyword is used to define a Ruby method, followed by the method name. Finally, we must use the end keyword to indicate that the Method has been declared.
Syntax:
Def methodName
Code…………………
End

You can also try this code with Online Ruby Compiler
Run Code
Now that we've grasped the syntax of methods let's talk about some naming conventions.
Methods Naming
The name of a method should always begin with a lowercase alphabet. Or else, it may be mistaken for a constant.
It might include letters, numbers, a low line, an underscore, or a character. In a multiword method name, there is a convention in which underscores are used to divide the words.
- A! (exclamation mark), a? (question mark), or a= equals sign can be used at the end of a method name.
- The bang methods (! at the end of the method name) are invoked and performed in the same way as any other method.
- By convention, methods that conclude in a question mark return boolean. However, they don't necessarily return true or false; instead, they frequently return an object to signify a true value.
- The equals sign at the end of a method indicates that it is an assignment method. The return value of assignment methods is disregarded, and the arguments are returned alternatively.
Arguments
A method in Ruby can take arguments.
- The method name is followed by a list of arguments.
- The use of parentheses around the arguments is not necessary.
- A comma is used to separate multiple arguments.
- The arguments are positional.
For example: -
Def add_values(num1, num2)
Return num1 + num2
End

You can also try this code with Online Ruby Compiler
Run Code
When calling the Method in the example above, you must give two parameters. The arguments are local variables within the method body. After that, the procedure will take two parameters and return the result. If you feed this method 10 and 30, it will return 40.
When defining methods, you may wish to structure them so that they always work, regardless of whether or not they are provided arguments. With default arguments, this is achievable.
Default Arguments
Argument default values can be specified. The default value does not have to come first; rather, default arguments must be grouped collectively.
Def add_values(num1,num2= 10)
num1 + num2
End

You can also try this code with Online Ruby Compiler
Run Code
We've learned about arguments and default arguments so far. Let's look at how to call methods now.
Call a Method
Methods that belong to (are defined on) objects in Ruby can be used (called) by preceding the method name with a dot, as in:
Object.method

You can also try this code with Online Ruby Compiler
Run Code
There are two ways to call a method,
- With parentheses
- Without parentheses
You can use the following syntax to call methods with parentheses:
UserMethod()

You can also try this code with Online Ruby Compiler
Run Code
You can use the following syntax to call methods without parentheses:
UserMethod

You can also try this code with Online Ruby Compiler
Run Code
Let's understand more with an example:-
With parentheses
Def add_values(num1, num2)
Return num1 + num2
End
puts(add_values(10, 20))

You can also try this code with Online Ruby Compiler
Run Code
Output:
30
In this example, we used the parenthesis approach to combine two integers, resulting in an output of 30.
Without parentheses
Def print_string()
puts(“Without parentheses”)
end
print_string()

You can also try this code with Online Ruby Compiler
Run Code
Output:
Without parentheses
We utilized a straightforward way (without parenthesis) in this example and obtained the statement we wanted as a result which is “ Without parentheses” in this example.
Overriding
Ruby merely redefines the def keyword when it is used (whether the Method already exists or not). This is referred to as overriding.
Let's understand it with an example.
def language
puts("We are doing Integration ")
end
def language
puts("We are doing Probability ")
end
# Now call the Method
language

You can also try this code with Online Ruby Compiler
Run Code
Output:
We are doing Probability
In this example, the method from the corresponding class came into action, which means that one overrode the other. If a method in the superclass has the same name as a method in its subclass, then running the method will execute the method of the corresponding class, Which is “ We are doing Probability” in this case.
Frequently Asked Questions
How do you define Ruby Methods?
The Ruby approach prevents us from retyping the same code in the software. Methods in Ruby are equivalent to functions in other programming languages.
How to use Ruby methods.
We must first define a Ruby method before we can utilize it. The keywords def and end are used to define it. The name of the Method should always begin with a lowercase letter.
What are some fundamental naming conventions for methods?
The following are some fundamental naming conventions to keep in mind while naming:
- The name of a method should always begin with a lowercase alphabet. Or else, it may be mistaken for a constant.
- It might include letters, numbers, a low line, an underscore, or a character. In a multiword method name, there is a convention in which underscores are used to divide the words.
When calling methods with or without parenthesis, what is the syntax?
While calling methods with parentheses, we should use the following syntax:
UserMethod()

You can also try this code with Online Ruby Compiler
Run Code
While calling methods without parentheses, we should use the following syntax:
UserMethod

You can also try this code with Online Ruby Compiler
Run Code
What is Method overriding in Ruby?
Method overriding simply means that two methods are declared inside the same scope and are utilized to do distinct tasks. This capability is available in an Object-Oriented Language with Inheritance support.
Overriding takes place in the manner that the Method of derived class or subclass replaces or overrides the implementation of the Derived class method.
Conclusion
In this article, we learned about Ruby Methods, including what they are, how to name them, and how to call them with and without parenthesis. After reading about Ruby Methods, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Object Marshalling in Ruby, Tainting Objects in Ruby, and Object References in Ruby.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
