From: Lyle Johnson Date: 2002-04-20T07:30:07+09:00 Subject: Re: String to method name > Thanks a lot... it does help, a little. The problem is, I am trying to > feed this stuff into (at the moment) a global variable in a block, like > this: > > FILE.each_line do |line| > (badGuyType,badGuyShow,badGuy) = line.scan(/^(\S+)\s(\S+)\s(.*)$/).flatten > $senslessNames[badGuyType] = {badGuyShow => badGuy} > end > > where the file looks like lines of this: > > mutantMonkey powerPuffGirls Mojo Jojo > mutantMonkey planetOfTheApes Ape Number 3 > evilCat powerPuffGirls kitty > evilCat planetOfTheApes Ninja Kat 5000 > Try this block instead: # Be sure to initialize it to an empty hash $senslessNames = {} # Read file and store inputs FILE.each_line do |line| badGuyType, badGuyShow, badGuy = line.scan(/^(\S+)\s(\S+)\s(.*)$/).flatten if $senslessNames.has_key? badGuyType $senslessNames[badGuyType][badGuyShow] = badGuy else $senslessNames[badGuyType] = { badGuyShow => badGuy } end end The problem with your original version is that for a given "badGuyType" key, you're always overwriting the previously created hash for that key. The line: $senslessNames[badGuyType] = { badGuyShow => badGuy } creates a new Hash object (the RHS of this assignment) with only one key-value pair and stores that at the specified location in the $senslessNames hash. My modified version (above) checks to see if the bad guy type we just read is already present as a key in the $senslessNames hash. If it is, we just add a new key-value pair to the already-existing hash at that location. If it isn't, we know it's safe to create a new hash as before. Now might be an excellent time for you to check out the chapter in "Programming Ruby" about different container types, including the Hash: http://www.rubycentral.com/book/tut_containers.html Hope this helps, Lyle