0

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?

3
  • MAybe this will helps ruby-doc.org/core-1.9.3/Struct.html Commented Mar 21, 2017 at 16:26
  • Your question is unclear. What do you want the result of your code to be? What does test have to do with it? Why are you assigning to test and then never ever using the result? Commented Mar 21, 2017 at 18:41
  • oop my bad, i'll remove it. Commented Mar 21, 2017 at 23:49

1 Answer 1

4

Use the splat operator:

arrs = [[:key1, :key2, :key3, :key4],[:key6, :key7]]
arrs.map { |a| Struct.new(*a) }
=> [#<Class:0x007fa833e25738>, #<Class:0x007fa833e1fa18>]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.