From: Todd Benson Date: 2008-03-05T16:26:32+09:00 Subject: Re: Newbea question again? On Tue, Mar 4, 2008 at 10:29 PM, Ibrahim Dogru wrote: > Hello Ruby Developers, > > I have tiny problem about comparison. I have a array and hash. I want to > compare array elements with hash keys. > more details: > this is my hash, I create it from yaml file > my yaml file > template1: > template_of_roster3: template_of_roster2 > template_of_roster4: template_of_roster3 > > > @get_config=YAML.load(File.open("#{PATH_IM}/config/converter.yml")) > @get_config == Hash > @my_hash==Hash > @enterprises=@get_config["template1"] > > and my array > @my_array=["template_of_roster","template_of_roster2","template_of_roster3"] > > my question here is that how can I compare array with hash keys and how > can I can send warning message to user? > @my_array.eql?(@my_hash.key)==false > warn "warning message" > > something like this. > > thanks.. You have a strange structure here. You have values that act as keys. 4 -> 3 and 3 -> 2. But, if it won't be circular, and your hashes are truly nested, then, here's a cowboy recursive hack... @b = false def check h, key h.each { |k, h| @b ||= h.keys.include?(key) || check(h, key) rescue false; @b} end hsh = YAML.load_file("your_file.yml") puts check(hsh, "template_of_roster") puts check(hsh, "template_of_roster3") => false => true If your hashes are not nested (i.e. you have one level that you are concerned about), then all you should just use use h["template1"].keys.include? hth, Todd