0
input = {:a=>{:type=>"list", :values=>[{:type=>"b", :id=>"123"}, {:type=>"b", :id=>"456"}]}}

output should be as follows

{:new_name=>"123,456"}

I tried using inject like this.Not sure what to include inside the block

input.tap do |e|
 e[:a].try(:[], :values).inject({}) do |h, obj|
   h[:new_name] = **
 end
end
0

2 Answers 2

1

You are given:

input = {:a=>{:type=>"list",
              :values=>[{:type=>"b", :id=>"123"},
                        {:type=>"b", :id=>"456"}
                       ]
             }
        }

Let's first simplify:

a = input[:a][:values]
  #=> [{:type=>"b", :id=>"123"},
       {:type=>"b", :id=>"456"}]

Now pull out the strings we need:

b = a.map { |h| h[:id] }
  #=> ["123", "456"]

Lastly,

{ "new_name" => b.join(',') }
  #=> {"new_name"=>"123,456"}

Putting this all together,

{ "new_name" => input[:a][:values].map { |h| h[:id] }.join(',') }
  #=> {"new_name"=>"123,456"}
Sign up to request clarification or add additional context in comments.

Comments

0
{:new_name => input.values.first[:values].map{|hash| hash[:id]}.join(',')}
# => {:new_name=>"123,456"}

This works only if your input hash has always the same structure.

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.