Forum: Ruby false or true == true .... WTF?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4618d7fb1dc6d939414f36adbb2a187c?d=identicon&s=25 Shea Martin (Guest)
on 2007-04-05 19:36
(Received via mailing list)
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 ""
[/code]

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
4618d7fb1dc6d939414f36adbb2a187c?d=identicon&s=25 Shea Martin (Guest)
on 2007-04-05 19:41
(Received via mailing list)
Shea Martin wrote:
> puts "has_returns = #{has_returns}, has_params = #{has_params},
> [/output]
> ~S
I can get templated to the correct value by doing this:
[code]
templated = false
if nreturns > 0 or nparams > 0
  templated = true
end
[/code]

But I should not have to do this.  Annoying.

~S
Ac0085dae0703db56ad7f8cb9e1798ba?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2007-04-05 19:47
(Received via mailing list)
Shea Martin 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.
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2007-04-05 20:01
(Received via mailing list)
On Apr 5, 1:33 pm, Shea Martin <s...@null.net> 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
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-04-05 20:10
(Received via mailing list)
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
> [/code]

In

  t = nr > 0 or np > 0

the binding is

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

Use

  t = nr > 0 || np > 0

Bertram
This topic is locked and can not be replied to.