arraysrubyhash-of-hashes

Printing out multiple hash values from an array


Hi I am trying to push out multiple hash values within a function. I can only get the first hash to return. I would like to get all hash values to return so that I can format them after I can pull out the data but so far I am only able to get the value of the first hash. Thank you in advance for the help here is where I am so far.

def list(names)
names.each do|name|
 name.each do |key,value|
   return "#{value}"
 end
end
end

Solution

  • def list(names)
        values = []
        names.each do|name|
            name.each do |key,value|
                values << value
            end
        end
        values
    end
    

    You need to keep all the values, instead of return the first value immediately.


    Ruby itself has many methods, you can do this very case in one-liner manner.

    def list(names)
        names.map(&:values).flatten
    end