* [ruby-core:72390] [Ruby trunk - Bug #11848] [Open] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
@ 2015-12-19 17:06 ` prodis
2015-12-19 17:06 ` [ruby-core:72391] [Ruby trunk - Feature #11848] " prodis
` (6 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: prodis @ 2015-12-19 17:06 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been reported by Fernando Hamasaki de Amorim.
----------------------------------------
Bug #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848
* Author: Fernando Hamasaki de Amorim
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v:
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:72391] [Ruby trunk - Feature #11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
2015-12-19 17:06 ` [ruby-core:72390] [Ruby trunk - Bug #11848] [Open] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass prodis
@ 2015-12-19 17:06 ` prodis
2015-12-19 19:49 ` [ruby-core:72393] [Ruby trunk - Feature #11848] [Feedback] " ruby-core
` (5 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: prodis @ 2015-12-19 17:06 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Fernando Hamasaki de Amorim.
Tracker changed from Bug to Feature
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-55678
* Author: Fernando Hamasaki de Amorim
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:72393] [Ruby trunk - Feature #11848] [Feedback] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
2015-12-19 17:06 ` [ruby-core:72390] [Ruby trunk - Bug #11848] [Open] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass prodis
2015-12-19 17:06 ` [ruby-core:72391] [Ruby trunk - Feature #11848] " prodis
@ 2015-12-19 19:49 ` ruby-core
2015-12-19 22:10 ` [ruby-core:72394] [Ruby trunk - Feature #11848] [Rejected] " matz
` (4 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: ruby-core @ 2015-12-19 19:49 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Marc-Andre Lafortune.
Status changed from Open to Feedback
Assignee set to Yukihiro Matsumoto
So many decisions in this seem completely arbitrary (and inconsistent), plus you don't give a use case, there's no way this would ever be accepted.
I'd suggest you build your own hash of true/false values and use that.
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-55680
* Author: Fernando Hamasaki de Amorim
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:72394] [Ruby trunk - Feature #11848] [Rejected] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
` (2 preceding siblings ...)
2015-12-19 19:49 ` [ruby-core:72393] [Ruby trunk - Feature #11848] [Feedback] " ruby-core
@ 2015-12-19 22:10 ` matz
2015-12-19 22:43 ` [ruby-core:72395] [Ruby trunk - Feature #11848] " eregontp
` (3 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: matz @ 2015-12-19 22:10 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Yukihiro Matsumoto.
Status changed from Feedback to Rejected
You forgot empty string/array/hash to be false. ;-)
But Ruby is not Python.
Matz.
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-55681
* Author: Fernando Hamasaki de Amorim
* Status: Rejected
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:72395] [Ruby trunk - Feature #11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
` (3 preceding siblings ...)
2015-12-19 22:10 ` [ruby-core:72394] [Ruby trunk - Feature #11848] [Rejected] " matz
@ 2015-12-19 22:43 ` eregontp
2015-12-20 14:18 ` [ruby-core:72414] " prodis
` (2 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: eregontp @ 2015-12-19 22:43 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Benoit Daloze.
I like the idea of having explicit conversion between booleans/integers, such that 0.to_b => false and true.to_i => 1.
!int.zero? can be a workaround for the first conversion but I only see a ternary condition for the second case.
Also, nil.to_i is already 0.
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-55682
* Author: Fernando Hamasaki de Amorim
* Status: Rejected
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:72414] [Ruby trunk - Feature #11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
` (4 preceding siblings ...)
2015-12-19 22:43 ` [ruby-core:72395] [Ruby trunk - Feature #11848] " eregontp
@ 2015-12-20 14:18 ` prodis
2016-01-30 2:04 ` [ruby-core:73591] " andrew
2016-10-27 23:14 ` [ruby-core:77788] [Ruby trunk Feature#11848] " prodis
7 siblings, 0 replies; 8+ messages in thread
From: prodis @ 2015-12-20 14:18 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Fernando Hamasaki de Amorim.
The idea of ***to_b*** method is inspired in Rails and Swift:
* ActiveRecord::Type:Boolean in Rails:
https://github.com/rails/rails/blob/fc4084fc5bdf60c46824094f4d6a362304c047f6/activerecord/lib/active_record/type/boolean.rb#L10
https://github.com/rails/rails/blob/fc4084fc5bdf60c46824094f4d6a362304c047f6/activerecord/lib/active_record/connection_adapters/column.rb#L8
* NString#boolValue property in Swift:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html#//apple_ref/occ/instp/NSString/boolValue
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-55700
* Author: Fernando Hamasaki de Amorim
* Status: Rejected
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:73591] [Ruby trunk - Feature #11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
` (5 preceding siblings ...)
2015-12-20 14:18 ` [ruby-core:72414] " prodis
@ 2016-01-30 2:04 ` andrew
2016-10-27 23:14 ` [ruby-core:77788] [Ruby trunk Feature#11848] " prodis
7 siblings, 0 replies; 8+ messages in thread
From: andrew @ 2016-01-30 2:04 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Andrew Vit.
I've had to do this in a few places over the years myself:
~~~
TRUTHY_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'y', 'Y', 'yes', 'YES']
FALSY_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'n', 'N', 'no', 'NO']
~~~
> You forgot empty string/array/hash to be false. ;-)
I think the main reason for this is handling user input (from a file like CSV or other), so other types like Array/Hash are not expected there: just basic scalar values.
Still, it probably only makes sense for some specific situations.
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-56786
* Author: Fernando Hamasaki de Amorim
* Status: Rejected
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-core:77788] [Ruby trunk Feature#11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
` (6 preceding siblings ...)
2016-01-30 2:04 ` [ruby-core:73591] " andrew
@ 2016-10-27 23:14 ` prodis
7 siblings, 0 replies; 8+ messages in thread
From: prodis @ 2016-10-27 23:14 UTC (permalink / raw)
To: ruby-core
Issue #11848 has been updated by Fernando Hamasaki de Amorim.
Andrew Vit wrote:
> I've had to do this in a few places over the years myself:
>
> ~~~
> TRUTHY_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'y', 'Y', 'yes', 'YES']
> FALSY_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'n', 'N', 'no', 'NO']
> ~~~
>
> > You forgot empty string/array/hash to be false. ;-)
>
> I think the main reason for this is handling user input (from a file like CSV or other), so other types like Array/Hash are not expected there: just basic scalar values.
>
> Still, it probably only makes sense for some specific situations.
Andrew Vit, you can use **`wannabe_bool`** gem: https://github.com/prodis/wannabe_bool
----------------------------------------
Feature #11848: New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
https://bugs.ruby-lang.org/issues/11848#change-61093
* Author: Fernando Hamasaki de Amorim
* Status: Rejected
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
New ***to_b*** method converts **strings**, **symbols**, **numbers** and **nil** values in a **boolean** value.
***to_b*** method is available on ***String***, ***Symbol***, ***Numeric***, ***TrueClass***, ***FalseClass*** and ***NilClass*** classes.
## String
Returns ***true*** if string is one of **t**, **true**, **on**, **y**, **yes** or **1** values. Returns ***false*** otherwise.
Ignores trailing spaces and letter cases.
~~~ruby
't'.to_b # => true
'true'.to_b # => true
'on'.to_b # => true
'y'.to_b # => true
'yes'.to_b # => true
'1'.to_b # => true
''.to_b # => false
'0'.to_b # => false
'2'.to_b # => false
'-1'.to_b # => false
'f'.to_b # => false
'false'.to_b # => false
'off'.to_b # => false
'n'.to_b # => false
'no'.to_b # => false
'wherever'.to_b # => false
~~~
## Symbol
Same as ***symbol.to_s.to_b***.
~~~ruby
:'1'.to_b # => true
:t.to_b # => true
:true.to_b # => true
:on.to_b # => true
:y.to_b # => true
:yes.to_b # => true
:f.to_b # => false
:false.to_b # => false
:off.to_b # => false
:n.to_b # => false
:no.to_b # => false
:wherever.to_b # => false
~~~
## Numeric
Returns ***false*** if number is **zero**. Returns ***true*** otherwise.
### Integer
~~~ruby
0.to_b # => false
1.to_b # => true
2.to_b # => true
-1.to_b # => true
-2.to_b # => true
~~~
### Float
~~~ruby
0.0.to_b # => false
0.1.to_b # => true
1.0.to_b # => true
-0.1.to_b # => true
-1.0.to_b # => true
~~~
### BigDecimal
~~~ruby
require 'bigdecimal'
BigDecimal('0.0').to_b # => false
BigDecimal('0.1').to_b # => true
BigDecimal('1.0').to_b # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true
~~~
## NilClass
Returns ***false***.
~~~ruby
nil.to_b # => false
~~~
## TrueClass
Returns ***true***.
~~~ruby
true.to_b # => true
~~~
## FalseClass
Returns ***false***.
~~~ruby
false.to_b # => false
~~~
---Files--------------------------------
to_b_method.diff (9.63 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-10-27 22:44 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-11848.20151219170620@ruby-lang.org>
2015-12-19 17:06 ` [ruby-core:72390] [Ruby trunk - Bug #11848] [Open] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass prodis
2015-12-19 17:06 ` [ruby-core:72391] [Ruby trunk - Feature #11848] " prodis
2015-12-19 19:49 ` [ruby-core:72393] [Ruby trunk - Feature #11848] [Feedback] " ruby-core
2015-12-19 22:10 ` [ruby-core:72394] [Ruby trunk - Feature #11848] [Rejected] " matz
2015-12-19 22:43 ` [ruby-core:72395] [Ruby trunk - Feature #11848] " eregontp
2015-12-20 14:18 ` [ruby-core:72414] " prodis
2016-01-30 2:04 ` [ruby-core:73591] " andrew
2016-10-27 23:14 ` [ruby-core:77788] [Ruby trunk Feature#11848] " prodis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).