# Determine if part of a hash key exists

**URL:** <https://www.ruby-forum.com/t/determine-if-part-of-a-hash-key-exists/184979>\
**Category:** Rails\
**Created:** [March 11, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/determine-if-part-of-a-hash-key-exists/184979 "2010-03-11T12:14:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 11, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/determine-if-part-of-a-hash-key-exists/184979/1 "2010-03-11T12:14:54Z")

</div>

Hi,

I have a hash something like {:schools\_name =\>  
''test", :schools\_address =\> "test, etc…}

What i need to know is does any of the keys contain the word “schools”  
in it.

So

hash.has\_key? :schools\_name will return true

But how can i get it to return true if part of any key contains the  
word “schools”

I can think of a couple of ways using a loop but surely there has to  
be a quick 1 line way?

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 11, 2010, 12:51pm UTC](https://www.ruby-forum.com/t/determine-if-part-of-a-hash-key-exists/184979/2 "2010-03-11T12:51:49Z")

</div>

On 11 March 2010 11:13, johnnybutler7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hash.has\_key? :schools\_name will return true
> 
> But how can i get it to return true if part of any key contains the  
> word “schools”
> 
> I can think of a couple of ways using a loop but surely there has to  
> be a quick 1 line way?
> 
> JB

There’s a few things you can do with enumerable methods  
([module Enumerable - RDoc Documentation](http://ruby-doc.org/core/classes/Enumerable.html)) depending on what  
you want to know from the match (just whether it’s in there somewhere;  
exactly where it is; all the keys that match; etc)

Personally, I’d recommend looking at “select” and “detect” methods as  
a start, but here’s a line using a loop (like you said 😉  
Run this at the console to see all of the positions of the matches  
shown.

hash.keys.each { |hash\_key| puts hash\_key.to\_s =~ /schools/ }

But I _think_ this is what you’re after, but if not, hopefully it puts  
you on the right track:

hash.keys.select { |hash\_key| hash\_key.to\_s =~ /schools/ }

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 11, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/determine-if-part-of-a-hash-key-exists/184979/3 "2010-03-11T13:06:28Z")

</div>

Ye this is what i had come up with

hash.keys.collect {|k| k.to\_s.include?(‘schools’) }

like the regular expression better though,

cheers,

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 11, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/determine-if-part-of-a-hash-key-exists/184979/4 "2010-03-11T19:12:23Z")

</div>

h.each\_key.collect {|k| k if k.to\_s.include?(‘school’) }

irb(main):007:0\> h = {:school\_name =\> ‘RoR School’, :school\_address =\>  
‘RoR  
Street’}  
=\> {:school\_name=\>“RoR School”, :school\_address=\>“RoR Street”}  
irb(main):008:0\> h.each\_key.collect {|k| k if k.to\_s.include?(‘school’)  
}  
=\> [:school\_name, :school\_address]  
irb(main):009:0\>
