Monday, March 2, 2009

Display keys and values of hashes

Well before reading this Post let me tell you that author of this Idea is My friend "Michael Graff"

<% @hash.each do |key, value| %>
<%=h "#{key.to_s} #{value.to_s}" %>
<% end %>

You can iterate through hashes in many various ways, including sorting them:

<% @hash.keys.sort.each do |key| %>
<%=h "#{key.to_s} #{@hash[key]}" %>
<% end %>

No comments:

Post a Comment