# String comparison

**URL:** <https://rubytalk.org/t/string-comparison/23298>\
**Category:** ruby-talk\
**Created:** [14 December 2005 23:42 UTC](https://rubytalk.org/t/string-comparison/23298 "2005-12-14T23:42:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris\_Glawe](https://avatars.discourse-cdn.com/v4/letter/b/839c29/32.png) [@Boris\_Glawe](https://rubytalk.org/u/Boris_Glawe)\
**Post date:** [14 December 2005 23:42 UTC](https://rubytalk.org/t/string-comparison/23298/1 "2005-12-14T23:42:54Z")

</div>

Hi,

I have a class containing a string as an instance variable and I have a local  
variable containing a string.

I compare these two strings with '==' and with the .eql? method:

> **···**
>
> ###################################
> 
> obj.text = "hello world"  
> localtext = "hello world"
> 
> if obj.text == localtext  
> then  
> &nbsp;&nbsp;# do something  
> end
> 
> ###################################
> 
> The condition never became true in my program. Thus I added some debug output.  
> This is the the debugging code:
> 
> if not obj.text == localtext  
> then  
> &nbsp;&nbsp;print "\"#{obj.text}\" is not equal \"#{localtext}\"\n"  
> end
> 
> This is, what it gave me:
> 
> "hello world" is not equal "hello world"
> 
> It's not worth trying the example above, since it's not the actual code. The  
> actual code in my project is much more complex, that's why I used this example  
> to discribe my problem.
> 
> Fact is that when the string comparison does not succeed I get an output like
> 
> "hello world" is not equal "hello world"
> 
> Now it's your turn... What could cause this behaviour?
> 
> thanks and greets
> 
> Boris
> 
> P.S. I have cancelled two versions of this posting, since they contained some typos and syntax errors

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [15 December 2005 04:03 UTC](https://rubytalk.org/t/string-comparison/23298/2 "2005-12-15T04:03:25Z")

</div>

Hi --

> **···**
>
> On Thu, 15 Dec 2005, Boris Glawe wrote:
> 
> > Hi,
> > 
> > I have a class containing a string as an instance variable and I have a local  
> > variable containing a string.
> > 
> > I compare these two strings with '==' and with the .eql? method:
> > 
> > ###################################
> > 
> > obj.text = "hello world"  
> > localtext = "hello world"
> > 
> > if obj.text == localtext  
> > then  
> > &nbsp;&nbsp;# do something  
> > end
> > 
> > ###################################
> > 
> > The condition never became true in my program. Thus I added some debug output.  
> > This is the the debugging code:
> > 
> > if not obj.text == localtext  
> > then  
> > &nbsp;&nbsp;print "\"#{obj.text}\" is not equal \"#{localtext}\"\n"  
> > end
> > 
> > This is, what it gave me:
> > 
> > "hello world" is not equal "hello world"
> > 
> > It's not worth trying the example above, since it's not the actual code. The  
> > actual code in my project is much more complex, that's why I used this example  
> > to discribe my problem.
> 
> Well... but showing some working code that doesn't illustrate the  
> problem isn't going to help us help you 🙂
> 
> I wonder whether this .text method is return an object other than a  
> plain String -- perhaps an object with == redefined.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net
> 
> "Ruby for Rails", forthcoming from Manning Publications, April 2006!
