code_junkie

Ruby on Rails: hash.each {} issues

Nov 13th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. records_hash = records[:id].inject({}) { |result,h|
  2. if result.has_key?(h)
  3. result[h] += 1
  4. else
  5. result[h] = 1
  6. end
  7. result
  8. }
  9.  
  10. @test2 = records_hash.each{|key,value| puts "#{key} is #{value}"}
  11.  
  12. bozo is 3
  13. bubba is 4
  14. bonker is 5
  15.  
  16. bozo3bubba4bonker5
  17.  
  18. @test2 = records_hash.map { |k,v| "#{k} is #{v}" }
  19.  
  20. <% @records_hash.each do |k,v| %>
  21. <%= "#{k} is #{v}" %>
  22. <% end -%>
  23.  
  24. <% @records_hash.each do |k,v| %>
  25. <%= "#{k} is #{v}" %><br/>
  26. <% end -%>
  27.  
  28. <ul>
  29. <% @records_hash.each do |k,v| %>
  30. <li><%= "#{k} is #{v}" %></li>
  31. <% end -%>
  32. </ul>
  33.  
  34. @test2 = ""
  35. @test2 = records_hash.each { |k,v| s<< "#{k} is #{v}" }
Add Comment
Please, Sign In to add comment