Dimitry_N Dimitry_N - 5 months ago 8
Ruby Question

Array of strings as a attributes for new instance of a class - Ruby

How can I pass a an array of strings as attributes when creating new instance of a class? Say I want to do something like this:

Person.new(person_attrs) #<= Wrong number of arguments. Expected 3, got 1


where
person_attrs.inspect
returns
['foo', 'bar', 'baz']

Answer

It's expecting a hash when setting the attributes to an object. You are getting the Wrong number of arguments. Expected 3, got 1 error because it thinks the array as a whole is one argument (it does not care about it's contents).

Comments