From: Bruno Santana on
I ran the application and appears that:

-bash-4.0$ ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
pedra
pedra
alguem ganhou

the number 1 above is the content of the variable resultSorteio, the
first word "pedra" is the content of the variable maquina and the second
"pedra" is the content of the variable usuario. According to the code
below when both variables contain the value "pedra" it should be shown
"empatou", however appears "alguem ganhou". am I comparing the strings
properly?

if usuario == maquina then
puts("empatou")
else
puts("alguem ganhou")
end

The whole code is in my previous post. Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Jonathan Nielsen on
> I'm not so good in English but I understood that I shoud show you my
> whole code. I put a different if like what the guy above suggested and I
> worked. Thanks:

Let me apologize (unofficially) on behalf of the list/newsgroup/forum
for that... your question and code provided was just fine. Please
feel welcome to ask questions here.

-Jonathan Nielsen

From: Rick DeNatale on
On Mon, Apr 12, 2010 at 8:35 PM, Bruno Santana
<bruno_r_santana(a)yahoo.com.br> wrote:
> I ran the application and appears that:
>
> -bash-4.0$ ruby pedrapapeltesoura.rb
> Digite pedra, papel ou tesoura: pedra
> 1
> pedra
> pedra
> alguem ganhou
>
> the number 1 above is the content of the variable resultSorteio, the
> first word "pedra" is the content of the variable maquina and the second
> "pedra" is the content of the variable usuario. According to the code
> below when both variables contain the value "pedra" it should be shown
> "empatou", however appears "alguem ganhou". am I comparing the strings
> properly?
>
>  if usuario == maquina then
>                        puts("empatou")
>                else
>                        puts("alguem ganhou")
>                end
>
> The whole code is in my previous post. Thanks.

Well, it looks like the two values really aren't the same. Two observations.

If there is trailing whitespace in one of the values the puts
statements won't show this.

You're printing the values before you alter one of them.

To get a better debugging perspective try changing

puts(maquina)
puts(usuario)
usuario.chop

to

usuario.chop
p(maquina)
p(usuario)

Which will show the 'inspect' output of the two values right before
you compare them.



--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

From: Bruno Santana on
Hi Rick,

I did as you said and it worked. I descovered that there was a newline
character, look at it:

-bash-4.0$ ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
"pedra"
"pedra\n"
alguem ganhou

I solved it usind the method chomp:
usuario.chomp!
p(maquina)
p(usuario)

Now it's working:

-bash-4.0$ ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
"pedra"
"pedra"
empatou

Thank you and thank all of you. If you have any problem I'll come back
here.
--
Posted via http://www.ruby-forum.com/.