From: dblack@... Date: 2006-11-04T08:18:21+09:00 Subject: Re: Accessing a hash -- Beginner Hi -- On Sat, 4 Nov 2006, don wrote: > I want to access a hash via a gets command. A simplified > example, for a hash named, "test": > > print 'hash name: ' > hash_var = gets.chomp # enter 'test' here > > puts hash_var # <-- nil > puts test # <-- the contents of the hash > > One problem I see is hash_var is class String, while test is > class Hash. I cannot figure out how to make hash_var class > Hash. I'm not even sure if I am on the right track. I > would appreciate any help. Do you want to set a hash key, or retrieve a value using an existing key? For example: test = { "one" => 1, "two" => 2 } hash_key = gets.chomp puts test[hash_key] If you input "one", the program will print 1. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org