# Question about string compares with ruby

**URL:** <https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069>\
**Category:** Rails\
**Created:** [October 2, 2007, 1:56am UTC](https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069 "2007-10-02T01:56:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [October 2, 2007, 1:56am UTC](https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069/1 "2007-10-02T01:56:18Z")

</div>

Say I have two entries in my database:

1. Bartlett
2. Peabody

A user runs a search, and I want to return all matches to the name  
above. So the user types in “Bartlett Street”, and I want it to  
return #1 above. My ruby code looks like:

@term = params[:terms]  
@return = Return.find(:all, :conditions=\>[’(fieldName \<= ?)’, @term])

Note that because the user types in “Street” at the end, I can not do  
a explicit “==” comparison.

When I type in “Peabody” as the search term, why does it also return  
“Bartlett”. I understand that “B” comes before “P” in the alphabet,  
but I would expect the “\<=” to compare on each character in the  
string, not just the first … but it seems like it only checks the  
first character?

I guess it is more of a SQL syntax question, but does anyone have a  
better way to code this in ruby? Seems like such a simple thing to be  
able to do.

Thanks in advance!  
LAB

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [October 2, 2007, 2:03am UTC](https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069/2 "2007-10-02T02:03:18Z")

</div>

On 2 Oct 2007, at 00:55, LAB wrote:

> When I type in “Peabody” as the search term, why does it also return  
> “Bartlett”. I understand that “B” comes before “P” in the alphabet,  
> but I would expect the “\<=” to compare on each character in the  
> string, not just the first … but it seems like it only checks the  
> first character?

That’s a fairly standard lexicographic ordering: starting from the  
first character, compare pairs of characters until you find a pair  
that differ (or until you run out of pairs)

> I guess it is more of a SQL syntax question, but does anyone have a  
> better way to code this in ruby? Seems like such a simple thing to be  
> able to do.

Correct. You want to use the % sql wildcard, ie “where name like  
‘Barlett%’”

Fred

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [October 2, 2007, 2:33am UTC](https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069/3 "2007-10-02T02:33:10Z")

</div>

Hi LAB,

LAB wrote:

> I guess it is more of a SQL syntax question, but does anyone have a  
> better way to code this in ruby? Seems like such a simple thing to be  
> able to do.

Not tested, but expect you should experiment with LIKE… Probably end  
up  
with something like…

Return.find(:all, :conditions =\> 'fieldName LIKE %Q{@term}%)

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [October 2, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069/4 "2007-10-02T17:04:30Z")

</div>

try this:

term = params[:term].split.first  
unless term.empty?  
match = “%#{term}%”  
@return= Return.find(:all, :conditions=\>[’(fieldName like ?)’,term])  
end

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [October 2, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/question-about-string-compares-with-ruby/116069/5 "2007-10-02T16:49:15Z")

</div>

Thanks for those suggestions!! Inspired by those, I came up with a  
new solution …

In my case @term is always going to be longer than fieldName, so I  
would need to search for the wildcard as part of the fieldName, and  
not the @term. Instead, I decided that what I really need to do, is  
shorten @term. So if @term = “Bartlett Street”, I really only want to  
match against the first string there … “Bartlett” and lose the  
“Street” part of the @term.

Here’s what I did:

@term = params[:terms] //“Bartlett Street”  
@termArray = @term.split // [“Bartlett”, “Street”]  
@match = @termArray.first // "Bartlett

if (!@match.nil?)  
@return= Return.find(:all, :conditions=\>[’(fieldName like ?)’,  
@match])  
end

That seems to get me what I want … but it is a pretty unique case.  
I’m sure using the wildcard would provide a more general solution, but  
I wasn’t sure how to apply the wildcard to the “fieldName” in the SQL  
statement … I just kept getting syntax errors. This will work fine  
for me now.

Thanks again for all the feedback!

LAB
