# 1978 != 1978

**URL:** <https://www.ruby-forum.com/t/1978-1978/156067>\
**Category:** Ruby\
**Created:** [December 19, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/1978-1978/156067 "2008-12-19T23:35:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [December 19, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/1978-1978/156067/1 "2008-12-19T23:35:12Z")

</div>

I have these years that I am trying to compare to see if they are equal  
and when my code get to the right year in question where it is supposed  
to equate to true, it doesn’t. Here is my code:

\<% 1900.upto(year) do |i| %\>  
\<%= option\_tag(i, i, @user.dobyear.to\_s == i.to\_s ? true : false) %\>  
\<% end %\>

When I do some ‘puts’ I get a condition where ‘1978’ == ‘1978’ and that  
results in a false value and I can’t figure out why. Why in Ruby does  
this happen? Thansk,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [December 19, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/1978-1978/156067/2 "2008-12-19T23:49:12Z")

</div>

Hi –

On Sat, 20 Dec 2008, Shandy N. wrote:

> I have these years that I am trying to compare to see if they are equal  
> and when my code get to the right year in question where it is supposed  
> to equate to true, it doesn’t. Here is my code:
> 
> \<% 1900.upto(year) do |i| %\>  
> \<%= option\_tag(i, i, @user.dobyear.to\_s == i.to\_s ? true : false) %\>  
> \<% end %\>

An aside: == returns true or false, so you don’t have to do the whole  
? true : false thing.

> When I do some ‘puts’ I get a condition where ‘1978’ == ‘1978’ and that  
> results in a false value and I can’t figure out why. Why in Ruby does  
> this happen? Thansk,

Are there spaces around @user.dobyear? I’m not sure what you mean  
about puts, except that puts returns nil, but I’m not sure how that  
fits in with your question.

David

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [December 20, 2008, 12:12am UTC](https://www.ruby-forum.com/t/1978-1978/156067/3 "2008-12-20T00:12:02Z")

</div>

- Shandy N., 2008-12-20, 07:27:

> does this happen?  
> It does not (checked). I suppose that the value of @user.dobyear only  
> looks as if it were ‘1978’ while in fact it contains some non-print  
> character. Try ‘p’ to verify.

And consider removing the ternary operator abuse. It makes not too much  
sense to have it results in true (false) if @user.dobyear.to\_s ==  
i.to\_s is true (false); I think using the truth value proper is the  
variant one should prefer.

Josef ‘Jupp’ Schugt

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [December 19, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/1978-1978/156067/4 "2008-12-19T23:56:10Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sat, 20 Dec 2008, Shandy N. wrote:
> 
> An aside: == returns true or false, so you don’t have to do the whole  
> ? true : false thing.
> 
> > When I do some ‘puts’ I get a condition where ‘1978’ == ‘1978’ and that  
> > results in a false value and I can’t figure out why. Why in Ruby does  
> > this happen? Thansk,
> 
> Are there spaces around @user.dobyear? I’m not sure what you mean  
> about puts, except that puts returns nil, but I’m not sure how that  
> fits in with your question.
> 
> David

With ‘puts’ I just meant that I did some output statements to see if in  
fact the values I was comparing were equal or not. As for the comparison  
when I took out the true and false parts it worked, thanks,

-S
