What does the * (asterisk) symbol do near a function argument and how to use that in others scenarios?

13,945

Solution 1

This is the splat operator, which comes from ruby (and is thus not rails specific). It can be applied in two ways depending on where it is used:

  • to "pack" a number of arguments into an array
  • to split up an array into an argument list

In your function, you see the splat operator used in the function definition. The result is that the function accepts any number of arguments. The complete argument list will be put into args as an array.

def foo(*args)
  args.each_with_index{ |arg, i| puts "#{i+1}. #{arg}" }
end

foo("a", "b", "c")
# 1. a   <== this is the output
# 2. b
# 3. c

The second variant would be when you consider the following method:

def bar(a, b, c)
  a + b + c
end

It requires exactly three arguments. You can now call this method like follows

my_array = [1, 2, 3]
bar(*my_array)
# returns 6

The splat applied in this case to the array will split it and pass each element of the array as an individual parameter to the method. You could do the same even by calling foo:

foo(*my_array)
# 1. 1   <== this is the output
# 2. 2
# 3. 3

As you can see in your example method, these rules do apply to block parameters in the same way.

Solution 2

This is a splat argument, which basically means that any 'extra' arguments passed to the method will all be assigned to *args.

Share:
13,945

Related videos on Youtube

user502052
Author by

user502052

Updated on May 24, 2022

Comments

  • user502052
    user502052 about 2 years

    I am using Ruby on Rails 3 and I would like to know what means the presence of a * operator near a function argument and to understand its usages in others scenarios.

    Example scenario (this method was from the Ruby on Rails 3 framework):

    def find(*args)
      return to_a.find { |*block_args| yield(*block_args) } if block_given?
    
      options = args.extract_options!
    
      if options.present?
        apply_finder_options(options).find(*args)
      else
        case args.first
        when :first, :last, :all
          send(args.first)
        else
          find_with_ids(*args)
        end
      end
    end