Ben Williams
Ben Williams

Reputation: 101

How to display data from hash into a table?

I have a hash, which contains information like this

{"Bananas"=>2, "Apples"=>3}

This hash regularly updates, such as when a new fruit gets added and clicked on. The number is the number of times it is clicked on.

I want to display a table which basically shows the name and the number of clicks

Something like this

@hash.each do |hash|
   %tr
      %th hash.fruit_name
      %th hash.clicks

Now I don't have access to .fruit_name and .clicks, which is why I'm not doing it like that, but I want the same result but from the hash

is this possible?

Upvotes: 0

Views: 214

Answers (1)

Martin
Martin

Reputation: 4222

Hash#each passes key and value on iteration. In your case key is a fruit_name and value is a clicks:

@hash.each do |fruit_name, clicks|
   %tr
      %th fruit_name
      %th clicks

Upvotes: 1

Related Questions