How to use Implicit Return Statement In Ruby
Return statemnet is not used here instead we directly define and implicitly returns the array, allowing the last expression to be the return value.
Syntax:
[inner_array, inner_hash]
Example:
In this example we create an array and a hash and then the method implicitly returns an array containing both inner array and inner hash. The result is stored in a variable result
and printed
# Method to return an array containing an array and a hash
def return_array
# Define an array
inner_array = [1, 2, 3]
# Define a hash
inner_hash = { name: "John", age: 30 }
# Implicit return of an array containing the array and hash
[inner_array, inner_hash]
end
# Call the method and store the result
result = return_array
# Print the result
p result
Output:
How to Return an Array that Contains an Array and Hash in Ruby Method?
In this article, we will learn how to return an array that contains an array and hash. We can return an array that contains an array and hash using the methods mentioned below:
Table of Content
- Using an Explicit Return Statement
- Using Implicit Return Statement
- Using Array Literal and Hash Constructor
Contact Us