# Re: Index and Query (#54)

**URL:** <https://www.ruby-forum.com/t/re-index-and-query-54/102317>\
**Category:** Ruby\
**Created:** [November 15, 2005, 12:23pm UTC](https://www.ruby-forum.com/t/re-index-and-query-54/102317 "2005-11-15T12:23:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kashia.buch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@kashia.buch](https://www.ruby-forum.com/u/kashia.buch)\
**Post date:** [November 15, 2005, 12:23pm UTC](https://www.ruby-forum.com/t/re-index-and-query-54/102317/1 "2005-11-15T12:23:11Z")

</div>

Hi,

looking at all those long subscriptions… couldn’t resist to make my  
own. probably not as complete/fast as those, but small and neat looking  
😃

should be pretty much self explenators. it’s using bignums for the index  
bitmaps.

have a nice day,  
Kash

* * *

docs = {  
:doc1 =\> “The quick brown fox”,  
:doc2 =\> “Jumped over the brown dog”,  
:doc3 =\> “Cut him to the quick”  
}

# building word list

lst = docs.map {|k,v| v.split(/[^\w’]+/) }.flatten.uniq

# building index

index = docs.inject(Hash.new(0)) do |hash, (k, v)|  
lst.each { |x| hash[k] |= 2\*\*lst.index(x) if v.index(x) }  
hash  
end

# query the string

q = ARGV[0] || “Cut”

res = index.map { |k,v| k if (v & 2\*\*lst.index(q)) \> 0 }.compact  
p res
