From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (smtp.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 3516D1B0001A for ; Fri, 28 Oct 2016 07:44:53 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 23036B5DA27 for ; Fri, 28 Oct 2016 08:15:53 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 4B5AD18D1D42 for ; Fri, 28 Oct 2016 08:15:53 +0900 (JST) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 506AA1204CA; Fri, 28 Oct 2016 08:15:51 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id C029D1204C0 for ; Fri, 28 Oct 2016 08:15:47 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=X0qL+0NazQS/yEGsm4muBl/qciQ=; b=hsUB/9v2UUPEvJ+rOv QIUmvrH5U9trGUDampS0rkFBLPCQvi/Ve1Jo9p3Y4wP8qBCOp3wIz7WZO6D5ETKR JsCNcdRGVsatfrKnMtFyWXmcoFxUWV19/HPFnZwCCVMq60wcoX9ifjwOXrx9Xiak jOn0YS4uHj+bXeOi97ZLEt2dQ= Received: by filter0470p1mdw1.sendgrid.net with SMTP id filter0470p1mdw1-23955-58128A50-57 2016-10-27 23:14:24.608352106 +0000 UTC Received: from herokuapp.com (ec2-54-92-178-113.compute-1.amazonaws.com [54.92.178.113]) by ismtpd0002p1iad1.sendgrid.net (SG) with ESMTP id oFA9P5yYSJ2VtkZ4HYdMgA Thu, 27 Oct 2016 23:14:24.578 +0000 (UTC) Date: Thu, 27 Oct 2016 23:14:24 +0000 From: prodis@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 52644 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11848 X-Redmine-Issue-Author: prodis X-Redmine-Issue-Assignee: matz X-Redmine-Sender: prodis X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS4Xf3MgUFsUMLYGIskL86qbfhsIBTOA08VgKZ hUCvUSkElVI1nSxMzYyK7aI4MYK8Tteqw8gWVNqO20jxC/uiXHnzrPBJx24T/qmTU0L2+AU0MRem3g qObY9h/ooomJrmSMcf9STWHWr/tDmeozftKguS1wX6GW6frSLqiCsOqFUg== X-ML-Name: ruby-core X-Mail-Count: 77788 Subject: [ruby-core:77788] [Ruby trunk Feature#11848] New #to_b method for String, Symbol, Numeric, NilClass, TrueClass and FalseClass. X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "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/