# Comparison not working

**URL:** <https://www.ruby-forum.com/t/comparison-not-working/126879>\
**Category:** Rails\
**Created:** [January 23, 2008, 1:18am UTC](https://www.ruby-forum.com/t/comparison-not-working/126879 "2008-01-23T01:18:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 23, 2008, 1:18am UTC](https://www.ruby-forum.com/t/comparison-not-working/126879/1 "2008-01-23T01:18:37Z")

</div>

Hello,  
I am getting a very strange error… I am working on a view that  
compares a phone number in one model to a phone number in another model  
and displays the matches and non-matches. However, when I try to do a  
comparison using != it just pumps out hundreds of repeating lines. My  
view looks like this:

| \<% for bes in @bes %\> \<% if bes.bes\_phonenumber.to\_s.size == 11 %\> \<% bes.bes\_phonenumber = bes.bes\_phonenumber.to\_s.slice(1..11) %\> \<% bes.bes\_phonenumber.to\_i %\> \<% end %\>

\<% for import in @prov %\>  
\<% if bes.bes\_phonenumber == import.prov\_service\_number %\>

 \<% end %\>

\<% end %\>  
\<% end %\>

| **Matches** |
| \<%= bes.bes\_displayname %\> | \<%= bes.bes\_phonenumber %\> |

 | \<% for bes in @bes %\> \<% if bes.bes\_phonenumber.to\_s.size == 11 %\> \<% bes.bes\_phonenumber = bes.bes\_phonenumber.to\_s.slice(1..11) %\> \<% bes.bes\_phonenumber.to\_i %\> \<% end %\>

\<% for import in @prov %\>  
\<% if bes.bes\_phonenumber == import.prov\_service\_number returns false  
%\>

 \<% end %\>

\<% end %\>  
\<% end %\>

| **Non-Matches** |
| \<%= bes.bes\_displayname %\> | \<%= bes.bes\_phonenumber %\> |

 |

The first one works perfectly (I think) but the second just pumps out  
about 100 rows of each bes\_displayname and bes\_phonenumber. Does anybody  
know what is causing the error? I tried to find a comparison operator in  
Ruby to say “not equals” but came up short… Is != not part of Ruby?

Thanks,

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 23, 2008, 1:57am UTC](https://www.ruby-forum.com/t/comparison-not-working/126879/2 "2008-01-23T01:57:10Z")

</div>

oops, what I posted was the wrong thing, sorry…

The relevant part of the code is this:

```
<% for bes in @bes %>

```

\<% if bes.bes\_phonenumber.to\_s.size == 11 %\>  
\<% bes.bes\_phonenumber = bes.bes\_phonenumber.to\_s.slice(1…11) %\>  
\<% bes.bes\_phonenumber.to\_i %\>  
\<% end %\>

\<% for import in @prov %\>  
\<% if bes.bes\_phonenumber != import.prov\_service\_number %\>

 \<%= bes.bes\_displayname %\> \<%= bes.bes\_phonenumber %\> \<%= import.prov\_service\_number %\> \<% end %\>

\<% end %\>  
\<% end %\>

Thanks!

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 23, 2008, 11:48am UTC](https://www.ruby-forum.com/t/comparison-not-working/126879/3 "2008-01-23T11:48:53Z")

</div>

On 23 Jan 2008, at 00:18, Jeff M. wrote:

> 

Random guess: you think bes.bes\_phonenumber.to\_i is actually changing  
bes.bes\_phonenumber. It isn’t

> \<% for import in @prov %\>  
> \<% if bes.bes\_phonenumber == import.prov\_service\_number returns false  
> %\>

Not sure what you’re on about here, ‘returns’ isn’t a ruby thing, and  
rather meaningless in the context of a view.  
All this messing around with slicing strings etc. either belongs in a  
model (if this is a data normalization thing) or in a helper of some  
sort, as it makes views confusing and/or messy

Fred

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [January 23, 2008, 3:59am UTC](https://www.ruby-forum.com/t/comparison-not-working/126879/4 "2008-01-23T03:59:39Z")

</div>

What is bes and prov? Please don’t shorten the variable names as it  
makes it  
harder for other programmers to understand what the code is about.

You can do a select method call on the @prov collection and put the  
condition in there, without the if.

@prov.select { |prov| prov.number != bes.number }

On Jan 23, 2008 11:27 AM, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \<% end %\>  
> \<% end %\>
> 
> ## Thanks!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
