Double Splat operator in ruby

In Ruby, the double splat operator (**) is used to convert a hash into a list of keyword arguments. This allows developers to pass a hash of values as keyword arguments to a method, and to expand the hash into a list of individual arguments.

Here is an example of using the double splat operator to convert a hash into a list of keyword arguments:

# Define a method that takes keyword arguments
def my_method(a:, b:, c:)
  # Print the keyword arguments
  puts a, b, c
end

# Define a hash of values
values = { a: 1, b: 2, c: 3 }

# Pass the hash to the method, using the double splat operator to expand it into a list of keyword arguments
my_method(**values)

In this example, the my_method method takes three keyword arguments, a, b, and c. The values hash is defined, and is passed as an argument to the my_method method, using the double splat operator to expand the hash into a list of keyword arguments. When the my_method method is called, it prints the expanded list of keyword arguments to the console.

Here is another example of using the double splat operator to convert a hash into a list of keyword arguments:

# Define a method that takes keyword arguments
def my_method(a:, b:, c:)
  # Print the keyword arguments
  puts a, b, c
end

# Pass the hash directly to the method, using the double splat operator to expand it into a list of keyword arguments
my_method(**{ a: 1, b: 2, c: 3 })

In this example, the my_method method is called with a hash as an argument, using the double splat operator to expand the hash into a list of keyword arguments. When the my_method method is called, it prints the expanded list of keyword arguments to the console.

Overall, the double splat operator is a useful operator in Ruby that allows developers to convert hashes into lists of keyword arguments, and to pass these lists of keyword arguments to methods. By using the double splat operator, developers can pass an arbitrary number of values as keyword arguments to a method, and can expand hashes into lists of keyword arguments.

Did you find this article valuable?

Support Matheus Puppe Blog by becoming a sponsor. Any amount is appreciated!