2

I have following output available in a variable test

#<someobject customer=[#<someobject product=[#<someobject id='ABC123'>, #<someobject id=''>], id='ADE343'>]>

I am trying to convert its result as follows:

#<someobject customer=[#<someobject product=['ABC123','DEF143'], id='ADE343'>]>

I can achieve that using following but looks like overkill

test1 = test.customer.map { |p| p.product }.flatten.map { |e| e.id }
test.customer.map { |p| p.product = test1 }
test

Is there any better way to do this?

2
  • is this rails or just ruby? before customer.product returned an array of product objects I think but now you want it to return an array of strings? Commented Oct 1, 2018 at 19:27
  • Can you show the associated class definition (with initialize the only method), in part so we can test? Commented Oct 1, 2018 at 19:32

1 Answer 1

2
test.customer.tap { |obj| obj.product.map!(&:id) }
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.