0

I have the following array:

 my_tst = [
    [
        {
            "name": "shield",
            "version": "8.6.3"

        },
        {
            "name": "bosh-dns",
            "version": "1.17.0"
        },
        {
            "name": "nessus_agent",
            "version": "1.0.24"
        },
        {
            "name": "node-exporter",
            "version": "4.2.0"
        },
        {
            "name": "syslog",
            "version": "11.6.1"
        }
    ],
    [
        {
            "name": "shield",
            "version": "8.6.3"
        },
        {
            "name": "bosh-dns",
            "version": "1.16.0"
        },
        {
            "name": "nessus_agent",
            "version": "1.0.24"
        },
        {
            "name": "node-exporter",
            "version": "4.2.0"
        },
        {
            "name": "syslog",
            "version": "11.6.1"
        }
    ]
]

I am trying to loop through the array and output only the values of name. I used this loop:

my_tst["name"].each do |run|
    p run
end

The loop is returning an Error:

TypeError: no implicit conversion of String into Integer

How do I output all values in the nested array?

3
  • 1
    Yours is a pure-Ruby question so the reference to and tag "Sinatra" are not relevant. Nor is there a need for the tag "arrays" (or other common Ruby objects), as it would not likely be used as a filter when searching questions. The reason is that its inclusion in filters would exclude many questions that deal with arrays but do not have that tag. Here the lone tag "ruby" appears sufficient. Commented Jan 10, 2020 at 18:35
  • Please read "How to Ask" and the linked pages and "Stack Overflow question checklist". We need to see what you expect as a result and to see your attempt at solving the problem; Don't expect us to generate a solution randomly. SO helps fix broken code, we don't write code. Commented Jan 12, 2020 at 5:23
  • Your array of arrays of hashes is suspicious and looks like the data isn't being generated correctly as an array of hashes would suffice and be easier to work with; See "What is the XY problem?". Commented Jan 12, 2020 at 5:29

1 Answer 1

4

You're trying to use [] in an array, which is meant to be used passing a numeric parameter in order to access its elements by their index. You're passing a string, which is the way you get values from hashes, and there's the problem.

You have an array of arrays containing hashes (with an interesting indentation), so in that case you need to first iterate the "main" array, to be able to get the hashes over each array.

This is one way you can achieve that:

my_tst.each_with_object([]) do |e, arr|
  e.each { |f| arr << f[:name] }
end
# ["shield", "bosh-dns", "nessus_agent", "node-exporter", "syslog", "shield", "bosh-dns", "nessus_agent", "node-exporter", "syslog"]

Or:

data.flat_map do |e|
  e.map { |f| f[:name] }
end

Anyway, there's going to be a nested iteration.

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.