Function Multiple arguments in Ruby

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Ruby

In Ruby, you can define a method or function that takes multiple arguments to perform a specific task. You can pass any number of arguments to the method or function as long as their type is as expected by the method or function.

Here's an example of a method with two arguments:

def add_numbers(num1, num2)
  puts num1 + num2
end

# Call the method by passing two arguments
add_numbers(5, 10) # Output: 15

In the above example, the add_numbers method takes two arguments, num1 and num2, and adds them together to return the value.

You can also define a method with more than two arguments, as shown below:

def find_average(num1, num2, num3, num4, num5)
  sum = num1 + num2 + num3 + num4 + num5
  puts sum / 5.0
end

# Call the method by passing five arguments
find_average(2, 4, 6, 8, 10) # Output: 6.0

In the above example, the find_average method takes five arguments, num1 to num5, and calculates their average.

In addition to defining methods with multiple arguments, you can pass multiple arguments to an existing method as well:

def multiply_numbers(*numbers)
  result = 1
  numbers.each { |n| result *= n }
  puts result
end

# Call the method by passing multiple arguments
multiply_numbers(2, 3, 4) # Output: 24
multiply_numbers(5, 10, 20, 30) # Output: 30000

In this example, the multiply_numbers method takes an arbitrary number of arguments by using the splat operator *. The numbers parameter is an array that contains all the arguments passed to the method. The method then multiplies all the numbers in the array.