Forum: Ruby ? true : false returns nil?

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.
Adb4aa3b289e5a6d14e2f9b9632caccb?d=identicon&s=25 Russ Cordner (rcordner)
on 2009-04-15 19:03
I have the following code fragment:

i = 1
j = case i
 when 1
  true
 else
  false
 end

I would expect j to equal true after the run, but it equals nil. If I
change the return values to anything else but true and false, it works
as expected. Am I missing some special handling of true and false?
Thanx.
4a5ad89e5a16e46dc7155fa8c6cee83c?d=identicon&s=25 Don Wood (tinnidril)
on 2009-04-15 19:11
Russ Cordner wrote:
> I have the following code fragment:
>
> i = 1
> j = case i
>  when 1
>   true
>  else
>   false
>  end
>
> I would expect j to equal true after the run, but it equals nil. If I
> change the return values to anything else but true and false, it works
> as expected. Am I missing some special handling of true and false?
> Thanx.

I just tried this, and couldn't replicate your result.  What Ruby are
you using?

$ irb -v
irb 0.9.5(05/04/13)
$ irb
irb(main):001:0> i = 1
=> 1
irb(main):002:0> j = case i
irb(main):003:1>  when 1
irb(main):004:1>   true
irb(main):005:1>  else
irb(main):006:1*   false
irb(main):007:1>  end
=> true
irb(main):008:0> true ? true : false
=> true
irb(main):009:0> false ? true : false
=> false
irb(main):006:0> 1 ? true : false
=> true
irb(main):005:0> nil ? true : false
=> false
Adb4aa3b289e5a6d14e2f9b9632caccb?d=identicon&s=25 Russ Cordner (rcordner)
on 2009-04-15 19:19
>
> I just tried this, and couldn't replicate your result.  What Ruby are
> you using?
>

I'm using ruby 1.8.5 on Linux Centos under the Netbeans IDE.
Adb4aa3b289e5a6d14e2f9b9632caccb?d=identicon&s=25 Russ Cordner (rcordner)
on 2009-04-15 19:25
Russ Cordner wrote:
>>
>> I just tried this, and couldn't replicate your result.  What Ruby are
>> you using?
>>
>
> I'm using ruby 1.8.5 on Linux Centos under the Netbeans IDE.

Its a bug in the Netbeans IDE. The IDE says it's still nil, but a puts
reveals the correct value. Thanx Don!
4a8632084a26acb9c096afd674ad773d?d=identicon&s=25 Darryl Pierce (mcpierce)
on 2009-04-16 16:41
(Received via mailing list)
Russ Cordner <russ.cordner@apptis.com> wrote:
> I would expect j to equal true after the run, but it equals nil. If I
> change the return values to anything else but true and false, it works
> as expected. Am I missing some special handling of true and false?
> Thanx.

(apologies for replying by email previously)

I tried the above and it worked for me as expected:

irb(main):023:0> i = 1
=> 1
irb(main):024:0> j = case i
irb(main):025:1> when 1
irb(main):026:1> true
irb(main):027:1> else
irb(main):028:1* false
irb(main):029:1> end
=> true
irb(main):030:0> j
=> true
This topic is locked and can not be replied to.