Reputation: 542
I have
arrs = [[:key1, key2, key3, key4],[:key6, key7]]
And I want to define Struct
by using those keys like this:
arrs.map do |arr|
Struct.new(arr)
end
but it does raise an error:
TypeError: no implicit conversion of Array into String
from (irb):26:in `new'
from (irb):26
So, do we have any way to initialize these keys in Struct
?
Upvotes: 0
Views: 282
Reputation: 13487
Use the splat operator:
arrs = [[:key1, :key2, :key3, :key4],[:key6, :key7]]
arrs.map { |a| Struct.new(*a) }
=> [#<Class:0x007fa833e25738>, #<Class:0x007fa833e1fa18>]
Upvotes: 4