0
class Test
  def initialize(*args)
    print "I got #{args.length} arguments!"
  end
end

arguments = ["Hello",100,30]

Test.new(arguments)

There, Test is only receiving one argument (an array). Would it be possible to pass the array's elements as the arguments for Test instead? So that Test's constructor identifies three arguments instead.

2 Answers 2

4

You want to use the splat operator, *.

class Test
  def initialize(*args)
    print "I got #{args.length} arguments!"
  end
end

arguments = ["Hello",100,30]

Test.new(*arguments)
1

Splat them: Test.new(*arguments)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.