-2
web_user_agents = {
    "linux" => [
            "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5) Gecko/2008122903 Gentoo Iceweasel/3.0.5",
            "Opera/5.0 (Linux 2.0.38 i386; U) [en]",
    ],
    "windows" => [
            "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko",
            "Opera/9.63 (Windows NT 5.2; U; en) Presto/2.1.1",
    ],
}

How can I iterate every element with key?

The output expected is:

"linux","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5) Gecko/2008122903 Gentoo Iceweasel/3.0.5"
"linux","Opera/5.0 (Linux 2.0.38 i386; U) [en]"
"windows","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko"
"windows","Opera/9.63 (Windows NT 5.2; U; en) Presto/2.1.1",
3
  • Your question is hard to decipher. Show us expected output, the code you've already written to try to generate that output, and the actual output. In the meantime, look up the documentation on the Ruby keys method: ruby-doc.org/core-2.1.1/Hash.html#method-i-keys Commented Apr 18, 2014 at 14:56
  • The output expected are 4 rows: "linux","Mozilla/5.0..." "linux","Opera/5.0..." "windows","Mozilla/5..." "windows","Opera/9.63...", Commented Apr 18, 2014 at 14:58
  • possible duplicate of How to iterate over a hash in Ruby? Commented Apr 18, 2014 at 15:09

3 Answers 3

2

You can iterate through the hash

web_user_agents.map { |k,v| v.map { |val| [k, val] } }.flatten

#=>[
#=> "linux","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5) Gecko/2008122903 Gentoo Iceweasel/3.0.5"
#=> "linux","Opera/5.0 (Linux 2.0.38 i386; U) [en]"
#=> "windows","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko"
#=> "windows","Opera/9.63 (Windows NT 5.2; U; en) Presto/2.1.1"
#=>]
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for one liner. I was thinking the same thing only down this way. web_user_agents.map{|k,v| [k].cycle(v.count).zip(v)}.flatten(1) so he would have an array of the key value pairs.
1

Assuming you don't really want the terminating comma on your last row, try:

web_user_agents.keys.each do |agent|
    web_user_agents[agent].each do |line|
        puts %Q("#{agent}","#{line}")
    end
end

Be aware that hashes are stored in non-deterministic order; if it matters to you how the keys come out, you may want to order them, or use an array instead of a hash to store them.

[EDIT: In recent versions of Ruby, hash key order is preserved.]

3 Comments

Actually hashes were stored in non-deterministic order only for ruby versions prior to 1.9.2, from then on, the insertion order is preserved. github.com/rubyspec/rubyspec/blob/master/core/hash/keys_spec.rb and stackoverflow.com/a/17355411/687142
Man, you turn your back for one second! Thanks for the info, I'll edit my answer.
The comma in the last row is permitted, but has no effect. Some add the comma in case they later decide to add other elements.
0

The following loops over the main hash.

 web_user_agents.each_pair do |k,v| 
   puts "#{k} => #{v}"
 end

You can nest this like so:

 web_user_agents.each_pair do |k,v|
   v.each do |value|
      puts "#{k} => #{value}"
   end
 end

3 Comments

This does not produce the expected output.
I think the OP is able to format the output on her/his own!
Would you say that if the OP were your project lead and you had been asked to format the output in a particular way? Why do it differently when it's just as easy to do it as requested?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.