0

I recently upgraded to Rails 6 with Ruby 3. I have this in a controller

my_object = MyObject.new(my_object_params, @header)

The object “initialize” method is defined like so

  def initialize(params, header)
    super(params)
    user&.header = header
  end

But now when attempting to initialize the object, I get the error

 ArgumentError:
   wrong number of arguments (given 2, expected 0..1)

What’s the proper way in Ruby 3 to pass multiple arguments to an object during initialization?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • 1
    Can you add the full stack trace? – OfirD May 05 '22 at 18:09
  • from which class are you inheriting? could it be its constructor doesn't accept parameters at all? maybe [this](https://stackoverflow.com/questions/4632224/super-keyword-in-ruby) will help – Yarin_007 May 07 '22 at 01:25

1 Answers1

0

You need to do this way:

  def initialize(params, header)
    super(**params) #Note that you need to splat out the params!
    user&.header = header
  end
Kali Vara
  • 43
  • 1
  • 6