MicFin
MicFin

Reputation: 2501

Ruby creating arrays based on a variable number of arguments passed to a method

I have a method that I would like to pass in a number of arguments for and then create a number of arrays based on how many arguments are passed in.

def method(*args)
  number_of_arrays = args.count
  for i in 1..number_of_args
    # create arrays
  end
  args.map do |arg|
      # do something and add to an array
  end
  # I should now have a number of different arrays based on how many arguments are passed in
  # do something with those arrays
end

Any guidance?

Upvotes: 0

Views: 616

Answers (1)

dexter
dexter

Reputation: 13583

def method(*args)
  arrays = Array.new(args.count) { [] }

  args.each_with_index do |arg, index|
      arrays[index] << # add some form of the arg to each array
  end
end

Upvotes: 2

Related Questions