Issue #7978 has been updated by alexeymuranov (Alexey Muranov).


phluid61 (Matthew Kerwin) wrote:
> funny_falcon (Yura Sokolov) wrote:
> > Well, yes: ruby is not C and false is not 0. But why false could not be
> >  converted to 0 by #to_i ?
> 
> That seems to imply that the reverse should hold, but (({!!0 => true})).

1. I do not think there is a rule that such fuzzy typecasting in Ruby has to be invertible:

"".to_i.to_s # => "0"

2. !!x is not one of Ruby type casting methods. If Ruby had a function Boolean(x), it would be natural to define it as !!x, but each class would need to define its own typecasting method. In my opinion, 0.to_b would have to be false.

> 
> Similarly, why should true.to_i return 1, and not -1 (as in Visual Basic) or 43 or 0 (which is also a truthy value)?

3. 1 is simpler than 43 or -1.  This is the usual convention of boolean algebra that 0 cannot be false and 1 is true. The logical AND becomes simply the multiplication (mod 2). If it is false that you have some object, you have 0 of it. :)
----------------------------------------
Feature #7978: boolean to_i
https://bugs.ruby-lang.org/issues/7978#change-37170

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=begin
The current behavior is the following:

  > nil.to_i
   => 0 
  > false.to_i
  NoMethodError: undefined method `to_i' for false:FalseClass

  > true.to_i
  NoMethodError: undefined method `to_i' for true:TrueClass

This does not look very consistent to me.  I think it could be useful to define (({false.to_i})) as 0 and (({true.to_i})) as 1.  I think those are fairly common numeric values for False and True.  These values as strings "0" and "1" are also commonly used in HTML forms to represent boolean values.
=end



-- 
http://bugs.ruby-lang.org/