# Newbea question again?

**URL:** <https://www.ruby-forum.com/t/newbea-question-again/131010>\
**Category:** Ruby\
**Created:** [March 5, 2008, 5:29am UTC](https://www.ruby-forum.com/t/newbea-question-again/131010 "2008-03-05T05:29:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 5, 2008, 5:29am UTC](https://www.ruby-forum.com/t/newbea-question-again/131010/1 "2008-03-05T05:29:53Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 5, 2008, 8:27am UTC](https://www.ruby-forum.com/t/newbea-question-again/131010/2 "2008-03-05T08:27:24Z")

</div>

On Tue, Mar 4, 2008 at 10:29 PM, Ibrahim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 5, 2008, 9:53am UTC](https://www.ruby-forum.com/t/newbea-question-again/131010/3 "2008-03-05T09:53:26Z")

</div>

> @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,

thank you very much…

> Todd

---

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 5, 2008, 10:46am UTC](https://www.ruby-forum.com/t/newbea-question-again/131010/4 "2008-03-05T10:46:10Z")

</div>

On Wed, Mar 5, 2008 at 2:53 AM, Ibrahim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thank you very much…  
> After looking at the documentation, I guess you can simply replace…

#keys.include?

…with…

#has\_key?

Todd

---

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 6, 2008, 1:46am UTC](https://www.ruby-forum.com/t/newbea-question-again/131010/5 "2008-03-06T01:46:26Z")

</div>

Todd B. wrote:

> On Wed, Mar 5, 2008 at 2:53 AM, Ibrahim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > thank you very much…  
> > After looking at the documentation, I guess you can simply replace…
> 
> #keys.include?
> 
> …with…
> 
> #has\_key?
> 
> Todd

thanks again.
