Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Access a hash item key by index (See related posts)

Here is how to access the hash item key by index in Ruby:

puts h.keys[0]

#=> fruit

The above code assumes we created the hash like so :
h = {"fruit" => "banana", "drink" => "water"}



Note: The hash item can be assigned an integer value as a key name e.g.

h = {12 => "grapes", 3 => "peach", 0 => "banana"}

You need to create an account or log in to post comments to this site.


Click here to browse all 6646 code snippets

Related Posts