False or true == true .... WTF?


#1

I have the following 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


#2

Shea M. wrote:

puts "has_returns = #{has_returns}, has_params = #{has_params},
[/output]
~S
I can get templated to the correct value by doing this:

templated = false
if nreturns > 0 or nparams > 0
  templated = true
end

But I should not have to do this. Annoying.

~S


#3

On Apr 5, 1:33 pm, Shea M. removed_email_address@domain.invalid wrote:

I have the following code:

[code]
templated = nreturns > 0 or nparams > 0
try:
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)
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


#4

Hi,

Am Freitag, 06. Apr 2007, 02:40:07 +0900 schrieb Shea M.:

I can get templated to the correct value by doing this:

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


#5

Shea M. wrote:

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.