# Partial Search

**URL:** <https://www.ruby-forum.com/t/partial-search/123642>\
**Category:** Ruby\
**Created:** [December 19, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/partial-search/123642 "2007-12-19T12:45:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruby\_tl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@ruby\_tl](https://www.ruby-forum.com/u/ruby_tl)\
**Post date:** [December 19, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/partial-search/123642/1 "2007-12-19T12:45:52Z")

</div>

I have created this method but I want to improve to be ale to search  
based on the keywords partially.

def self.search\_by\_keyword(businesses, searched\_keyword)  
businesses.select do |b|  
b.my\_keywords.include?(searched\_keyword)  
end  
end

* * *

## This are the objects

bsn0 = Business.new(“Romain”, “02089293323”, "22 lilly Road ")  
bsn0.add\_keyword(KEYWORDS[0])  
bsn0.add\_keyword(KEYWORDS[1])  
bsn0.add\_keyword(KEYWORDS[2])

bsn1 = Business.new(“Steve”, “02089293323”, "22 lilly Road ")  
bsn1.add\_keyword(KEYWORDS[0])  
bsn1.add\_keyword(KEYWORDS[2])

bsn2 = Business.new("Al ", “02085431234”, "25 Riverside Road ")  
bsn2.add\_keyword(KEYWORDS[1])

* * *

## How can I change the methpd in orrder to be able to perform the folowing task ?

Business.search\_by\_keyword([bsn0, bsn1, bsn2], KEYWORDS[3],true) # true  
here mean ‘exact match’

# will return nil

Business.search\_by\_keyword([bsn0, bsn1, bsn2], KEYWORDS[1],true) # true  
here mean ‘exact match’

# will return bsn2

Business.search\_by\_keyword([bsn0, bsn1, bsn2], KEYWORDS[1],false) #  
true here mean ‘partial match’

# will return bsn0 AND bsn2

Business.search\_by\_keyword([bsn0, bsn1, bsn2], [KEYWORDS[0],KEYWORDS[1]  
],true) # true here mean ‘exact match’

# will return nil

Business.search\_by\_keyword([bsn0, bsn1, bsn2], [KEYWORDS[0],KEYWORDS[2]  
],true) # true here mean ‘exact match’

# will return bsn1

Business.search\_by\_keyword([bsn0, bsn1, bsn2], [KEYWORDS[0],KEYWORDS[2]  
],false) # true here mean ‘partial match’

# will return bsn0 AND bsn1

---

<div class="post-metadata">

**Author:** ![ruby\_tl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@ruby\_tl](https://www.ruby-forum.com/u/ruby_tl)\
**Post date:** [December 19, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/partial-search/123642/2 "2007-12-19T13:20:19Z")

</div>

Well I changed the method, so that it can take the three parameters.

And my question would be How can I modify the ruby block code so that I  
can do the perform the matching.

def self.search\_by\_keyword(businesses, searched\_keyword\_as\_array,  
flag\_exact\_or\_partial\_match)  
businesses.select do |b|  
b.my\_keywords.include?(searched\_keyword)  
end  
end
