Home »
Ruby Tutorial
Ruby Methods
By IncludeHelp Last updated : November 16, 2024
Ruby methods
Generally, methods tell the behavior of objects. A method is a set of predefined code which can be invoked any time in the code by its name. The method reduces redundancy in the code as we don't have to write code again and again for the same task. When methods are the part of the class, it sets performance guidelines for the instances of that particular class.
In Ruby, the general syntax of the method is as follows:
def method_name
#code block
end
Example
# method to print text
def printn
puts "Hello friends!"
end
# calling the method
printn()
Output
Hello friends!
Above is syntax with no parameter list. If you want to specify the parameter list then follow the syntax given below:
Method with Parameters List
We can also pass the parameters while calling the method in Ruby programming like other programming languages, here is the syntax to define a method in Ruby with parameters list.
def method_name(value 1, value2, ...)
#expressions
end
Example
# method with a parameter
def printn(name)
puts "Hello #{name}"
end
# method calling with different values
printn("Amisha")
printn("Kamlesh")
printn("Satyam")
Output
Hello Amisha
Hello Kamlesh
Hello Satyam
Default Parameters
Ruby supports the feature of default parameters which means that if no value is provided to the method then the method will be invoked with default parameters in the following way:
def method_name(param1=value, param2=value, ...)
#code block
end
Example 1
# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
puts "Hello #{name}"
end
# method calling
printn() #invoking with default value
printn("Amisha")
printn("Kamlesh")
printn("Satyam")
Output
Hello Stranger
Hello Amisha
Hello Kamlesh
Hello Satyam
You can invoke a method by using () as well as you can call it without using it when you don't need to pass any parameter list like,
Example 2
# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
puts "Hello #{name}"
end
# another method definition without using ()
def sayhello
puts "Hi! You are at Includehelp.com"
end
# method calling with and without ()
printn
sayhello
printn("Amisha")
printn("Kamlesh")
printn("Satyam")
Output
Hello Stranger
Hi! You are at Includehelp.com
Hello Amisha
Hello Kamlesh
Hello Satyam
Example 3
# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
puts "Hello #{name}"
end
# method definition without using ()
def sayhello
puts "Hi! You are at Includehelp.com"
end
# method calling with ()
printn()
sayhello()
printn("Amisha")
printn("Kamlesh")
printn("Satyam")
Output
Hello Strange
Hi! You are at Includehelp.com
Hello Amisha
Hello Kamlesh
Hello Satyam
Both the above syntaxes are supported by Ruby. The compiler will not reflect an error in any of the cases.
Returning Values
This is one of the most important properties of methods that they return a value implicitly. That value is the last value of the expression like:
Example
# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
puts "Hello #{name}"
end
def sayhello
puts "Hi! You are at Includehelp.com"
end
# method calling
printn()
sayhello()
k = sayhello
print k
Output
Hello Stranger
Hi! You are at Includehelp.com
Hi! You are at Includehelp.com
In the above case, you can observe that we are not returning any value explicitly but still the value of the last expression is being returned from the method.
For explicit return, we can use 'return' statement at the last of the method definition in the following way:
Example
# method definition with return value
def marks
return 34
end
# method calling
m1=marks()
puts "#{m1}"
Output
34
You can also return multiple values through return statement like,
Example
# method definition returning multiple values
def marks
return 34,45,66
end
# method calling
m1=marks()
puts "#{m1}"
The compiler would not give an error while compiling the above code, the output will be like:
[34, 45, 66]
Implementation of Methods in Classes
Ruby is purely object-oriented. A class may have n number of methods to define the behavior of its objects. When the method is defined outside the class, its scope is private by default but if it is defined inside the class body, it is public by default.
You can access a method by creating an object of the class with the help of . operator like: instance_name.method_name
Example
Let us have a more clear vision with the help of an example:
# class definition
class Example
# class method that will return marks
def marks
return 34,45,66
end
# class method that will print greeting messahe
def greet
puts "Hello Fam!"
end
end
# object/instance creation
instance1 = Example.new
# calling methods
instance1.greet()
m = instance1.marks()
puts "The marks are: " "#{m}"
Output
Hello Fam!
The marks are: [34, 45, 66]