false or true == true .... WTF?

S

Shea Martin

I have the following code:
Code:
templated = nreturns > 0 or nparams > 0
puts "nreturns = #{nreturns}, nparams = #{nparams}, templated =
#{templated}"
has_returns = nreturns > 0
has_params = nparams > 0
templated2 = (nreturns or nparams)
puts "has_returns.class = #{has_returns.class}, has_params.class =
#{has_params.class}, templated2.class = #{templated2.class}"
puts "has_returns = #{has_returns}, has_params = #{has_params},
templated2 = #{templated2}"
puts ""

when nreturns is 0 and nparams is 1 or more I get this output:
[output]
nreturns = 1, nparams = 0, templated = true
has_returns.class = TrueClass, has_params.class = FalseClass,
templated2.class = Fixnum
has_returns = true, has_params = false, templated2 = 1
[/output]

Both 'templated2' and 'templated' should be true, not 0. I can't seem
to duplicate this in irb. Even in the debugger,
"p nreturns > 0 or nparams > 0" gives true, while "p templated" gives false.

This must be a bug in the interpreter is my guess? My ruby version is
1.8.4 win32.

Thanks,
~S
 
S

Shea Martin

Shea said:
I have the following code:
Code:
templated = nreturns > 0 or nparams > 0
puts "nreturns = #{nreturns}, nparams = #{nparams}, templated =
#{templated}"
has_returns = nreturns > 0
has_params = nparams > 0
templated2 = (nreturns or nparams)
puts "has_returns.class = #{has_returns.class}, has_params.class =
#{has_params.class}, templated2.class = #{templated2.class}"
puts "has_returns = #{has_returns}, has_params = #{has_params},
templated2 = #{templated2}"
puts ""

when nreturns is 0 and nparams is 1 or more I get this output:
[output]
nreturns = 1, nparams = 0, templated = true
has_returns.class = TrueClass, has_params.class = FalseClass,
templated2.class = Fixnum
has_returns = true, has_params = false, templated2 = 1
[/output]

Both 'templated2' and 'templated' should be true, not 0. I can't seem
to duplicate this in irb. Even in the debugger,
"p nreturns > 0 or nparams > 0" gives true, while "p templated" gives
false.

This must be a bug in the interpreter is my guess? My ruby version is
1.8.4 win32.

Thanks,
~S

I can get templated to the correct value by doing this:
Code:
templated = false
if nreturns > 0 or nparams > 0
templated = true
end

But I should not have to do this. Annoying.

~S
 
P

Phillip Gawlowski

Shea said:
This must be a bug in the interpreter is my guess? My ruby version is
1.8.4 win32.

irb(main):013:0> p nreturns > 0 or nparams > 0
false
=> true
irb(main):014:0> p nreturns > 0 || nparams > 0
true
=> nil

C:\>ruby -v
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-mswin32]


--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan.110mb.com/

Rule of Open-Source Programming #5:

A project is never finished.
 
C

ChrisH

I have the following code:
Code:
templated = nreturns > 0 or nparams > 0[/QUOTE]
try:
templated = (nreturns > 0 or nparams > 0)

[QUOTE]
puts "nreturns = #{nreturns}, nparams = #{nparams}, templated =
#{templated}"
has_returns = nreturns > 0
has_params = nparams > 0
templated2 = (nreturns or nparams)[/QUOTE]
Should this be:
templated2 = (has_returns or has_params) ??

With those changers i get:
nreturns = 0, nparams = 1, templated =true
has_returns.class = FalseClass, has_params.class =TrueClass,
templated2.class = TrueClass
has_returns = false, has_params = true,templated2 = true

Cheers
Chris
 
B

Bertram Scharpf

Hi,

Am Freitag, 06. Apr 2007, 02:40:07 +0900 schrieb Shea Martin:
I can get templated to the correct value by doing this:
Code:
templated = false
if nreturns > 0 or nparams > 0
	templated = true
end

In

t = nr > 0 or np > 0

the binding is

(t = nr > 0) or (np > 0)

Use

t = nr > 0 || np > 0

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top