← Back to Ruby Operators

Array Argument

The asterisk symbol (*) is used with the argument of a method definition to capture remaining positional arguments into an array.

Here is an example of a method that can accept any number of (positional) arguments and then receives them as an array to process.

def odd_finder(*items)
  items.each do |item|
    if item.odd?
      puts "Odd item: #{item}"
    end
  end
end

odd_finder(1, 2, 3, 4, 5)
# => Odd item: 1
# => Odd item: 3
# => Odd item: 5

References