8

This is theoretical question: is it possible to change FalseClass behavior to act like TrueClass? It is possible to override to_s, xor, &, | behavior but that is not enough.

If you like Test Driven Development, follow my colleague's suggestion:

puts "false is new true!" if false
puts "never happens" if true

assert false

Asserts won't work, would it? Is it possible to pass the test successfully?

4
  • Reminds me of C code obfuscation... ioccc.org
    – Lucero
    May 5, 2010 at 9:17
  • I have been developing an application for years and now I find it working not right, so I just want to make it right inversing the primary logics May 5, 2010 at 9:34
  • There are no assertion on your test :(
    – shingara
    May 5, 2010 at 9:37
  • Ok. I will add assert false But in long term I want to switch true with false. So how to assert true when true is false and false is true? May 5, 2010 at 9:46

2 Answers 2

9

It is not possible. One way to think about it is that there is no method Object#truthiness? that could be redefined.

In Ruby MRI, the truthiness test is the RTEST macro that is hardwired to mean anything but Qfalse and Qnil, the two constants corresponding to false and nil. You would have to change this to redefine what is "truthy" or not.

2

It is impossible, at least in the official Ruby implementation, as true and false logic is deep in the C parts (Qtrue and Qfalse). Making the assert pass would work, though, by overwriting assert. Also, you could use something like ruby2ruby to parse out all values, but than true would still not behave like false and statements like ![] would still return true. Also note that all other objects also behave like true in if statements and akin.

2
  • With official Ruby implementation, you're talking about MRI/YARV, right? May 5, 2010 at 23:40
  • Yes. In Maglev you could use Smalltalk's become. May 6, 2010 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.