0

I have an hash which has data something like :

details = [{
  :project_name=>"ABC", :project_id=>12345, :total_count=>1, 
  :stories=>[#<TrackerApi::Resources::Story id=987654>]
}, {
  :project_name=>"XYZ", :project_id=>54321, :total_count=>1, 
  :stories=>[#<TrackerApi::Resources::Story id=123456>]
}]

I want to insert another array with data such as :

sorter_Version = [["Abc ", "30 August 2017"], ["Not Accepted", "30 August 2017"]]

in details hash , but somehow not able to do it. can someone please help me out .Thanks in advance.

Output should be like :

details = [{
      :project_name=>"ABC", :project_id=>12345, :total_count=>1, 
      :stories=>[#<TrackerApi::Resources::Story id=987654>],
      :sorter_version=>["Abc ","30 August 2017"]
    }, {
      :project_name=>"XYZ", :project_id=>54321, :total_count=>1, 
      :stories=>[#<TrackerApi::Resources::Story id=123456>],
      :sorter_version=>["Not Accepted ","30 August 2017"]
    }]    
6
  • 1
    "somehow not able to do it" – can you show your approach, what have you tried? Was there an error or an unexpected result? Commented Sep 6, 2017 at 10:44
  • @Stefan i tried details.push(sorter_version), and then it got appended at last. but that is not what i wanted. Commented Sep 6, 2017 at 10:48
  • What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a minimal reproducible example. Stack Overflow is not a "write-my-code-for-me-service"! If you are looking for that, hire a programmer. Commented Sep 6, 2017 at 10:56
  • 1
    "somehow not able to do it" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Commented Sep 6, 2017 at 10:57
  • 1
    @JörgWMittag Next me i will make sure that i mention everything.... it was my first query so not aware of this thing ... and i know "Stack Overflow is not a "write-my-code-for-me-service"! " , needed a little helpto get the concept.Thanks anyway Commented Sep 6, 2017 at 11:05

1 Answer 1

1

Just use each_with_index:

details.each_with_index { |d, i| d[:sorter_version] = sorter_Version[i] }
#=> [{:project_name=>"ABC", :project_id=>12345, :total_count=>1, 
#     :stories=>["#<TrackerApi::Resources::Story id=987654>"], 
#     :sorter_version=>["Abc ", "30 August 2017"]}, 
#    {:project_name=>"XYZ", :project_id=>54321, :total_count=>1, 
#     :stories=>["#<TrackerApi::Resources::Story id=123456>"], 
#     :sorter_version=>["Not Accepted", "30 August 2017"]}]

Immutable version:

details.map.with_index { |d, i| d.merge(sorter_version: sorter_Version[i]) }
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.