ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: prodis@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:72391] [Ruby trunk - Feature #11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass.
Date: Sat, 19 Dec 2015 17:06:51 +0000	[thread overview]
Message-ID: <redmine.journal-55678.20151219170650.719ac36b18fd3b82@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11848.20151219170620@ruby-lang.org

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/

  parent reply	other threads:[~2015-12-19 16:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-55678.20151219170650.719ac36b18fd3b82@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).