×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Splat Arguments in Ruby

By IncludeHelp Last updated : December 01, 2024

Introduction to Methods in Ruby

We have learned how to work with methods in Ruby. We are very well aware of the fact that methods may or may not consume any arguments.

Methods with Arguments

Let us discuss the methods that consume arguments or have a predefined argument list. This argument list may contain any number of arguments, but with the compulsion that you will have to pass the same number of arguments with the method as specified in the argument list of the method.

Limitations of Predefined Argument Lists

However, this decreases the versatility of the code. What if you have created a method that adds three numbers, but the requirement of the time is to add only two numbers? In this case, your function will fail.

Overloading Methods

One way to handle this would be to overload the method, but the issue is how many times you can overload the method, as there may be numerous possibilities. Overloading the method multiple times will eventually make your code redundant.

Splat Arguments in Ruby

Ruby has a good concept of splat arguments that allows you to pass any number of arguments to a method. Splat arguments are of two types, which are discussed in the rest of the article.

Single Splat Arguments

Single splat argument is implemented with the help of * operator. You can pass Arrays with the help of them. Refer the syntax given below,

def method_name( *args)
  ...
end

Example

The following example will help you to understand its implementation,

def mul(*args)
  pro = 1
  args.each do |ar|
    pro = pro * ar
  end
  return pro
end

puts "Product is #{mul(12,44,55,33,22,55)}"
puts "Product is #{mul(1.2,4.4,5.5,3.3,2.2,5.5)}"
puts "Product is #{mul(1,2)}"
puts "Product is #{mul(100,45)}"

Output

Product is 1159567200
Product is 1159.5672000000002
Product is 2
Product is 4500

You can observe in the above code that we have created a method that is multiplying all the arguments which are passed in it. We are taking help from a splat operator which is allowing us to pass as many arguments we want. We have passed the different number of arguments in each method call. If this is not the case then we might have to define the different methods for the different number of arguments.

Double splat arguments

The concept of double splat argument was introduced in Ruby 2.0. The implementation is pretty similar to a single splat argument but an add-on feature that will also work for hashes. It is implemented with the help of ** operator. Following is the syntax that will tell you how can we use double splat arguments.

def show( **args)
  ...
end

Example

Now, let us go through its example for understanding it better.

def fruits (**fruits_and_color)
 fruits_and_color.each do |frt, color|
  puts "Fruits: #{frt}"
  puts "Color: #{color}"
   
  end
end

data1 = {
  "Kiwi": "Green",
  "Peach": "Pink",
  "Banana": "Yellow",
  "Grapes": "Green"
}

data2 = {
  "Kiwi": "Green",
  "Peach": "Pink",
  "Banana": "Yellow",
  "Grapes": "Green",
  "Watermelon": "Blue"
}

fruits data1
fruits data2

Output

Fruits: Kiwi
Color: Green
Fruits: Peach
Color: Pink
Fruits: Banana
Color: Yellow
Fruits: Grapes
Color: Green
Fruits: Kiwi
Color: Green
Fruits: Peach
Color: Pink
Fruits: Banana
Color: Yellow
Fruits: Grapes
Color: Green
Fruits: Watermelon
Color: Blue
=> {:Kiwi=>"Green", :Peach=>"Pink", :Banana=>"Yellow", :Grapes=>"Green", :Watermelon=>"Blue"

In the above code, you can observe that we have created two hashes data1 and data2. We have created a method fruits and we are passing these hashes with the different number of entries into the method.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.