If string, integer, symbol and nil values wanna be a boolean value, they can with the new to_b
method. Moreover, you can use WannabeBool::Attributes
module to create predicate methods in your classes.
<img src=“https://badge.fury.io/rb/wannabe_bool.png” alt=“Gem Version” /> <img src=“https://travis-ci.org/prodis/wannabe_bool.svg?branch=master” alt=“Build Status” /> <img src=“https://coveralls.io/repos/prodis/wannabe_bool/badge.png” alt=“Coverage Status” /> <img src=“https://codeclimate.com/github/prodis/wannabe_bool/badges/gpa.svg” alt=“Code Climate” />
gem 'wannabe_bool'
$ gem install wannabe_bool
to_b
method is available on String
, Symbol
, Integer
, TrueClass
, FalseClass
and NilClass
.
require 'wannabe_bool'
'1'.to_b # => true 't'.to_b # => true 'T'.to_b # => true 'true'.to_b # => true 'TRUE'.to_b # => true 'on'.to_b # => true 'ON'.to_b # => true 'y'.to_b # => true 'yes'.to_b # => true 'YES'.to_b # => true ' 1 '.to_b # => true ' t '.to_b # => true ' T '.to_b # => true ' true '.to_b # => true ' TRUE '.to_b # => true ' on '.to_b # => true ' ON '.to_b # => true ' y '.to_b # => true 'Y'.to_b # => true ' Y '.to_b # => true ' yes '.to_b # => true ' YES '.to_b # => true ''.to_b # => false '0'.to_b # => false '2'.to_b # => false 'f'.to_b # => false 'F'.to_b # => false 'false'.to_b # => false 'FALSE'.to_b # => false 'off'.to_b # => false 'OFF'.to_b # => false 'n'.to_b # => false 'N'.to_b # => false 'no'.to_b # => false 'NO'.to_b # => false 'not'.to_b # => false 'NOT'.to_b # => false 'wherever'.to_b # => false
:1.to_b # => true :t.to_b # => true :T.to_b # => true :true.to_b # => true :TRUE.to_b # => true :on.to_b # => true :ON.to_b # => true :y.to_b # => true :Y.to_b # => true :yes.to_b # => true :YES.to_b # => true :f.to_b # => false :F.to_b # => false :false.to_b # => false :FALSE.to_b # => false :off.to_b # => false :OFF.to_b # => false :n.to_b # => false :N.to_b # => false :no.to_b # => false :NO.to_b # => false :not.to_b # => false :NOT.to_b # => false :wherever.to_b # => false
1.to_b # => true 0.to_b # => false 2.to_b # => false
true.to_b # => true
false.to_b # => false
nil.to_b # => false
Creating predicate methods:
class Fake include WannabeBool::Attributes attr_accessor :name, :main, :published attr_wannabe_bool :main, :published end fake = Fake.new fake.main? # => false fake.published? # => false fake.main = true fake.main? # => true fake.published = 1 fake.published? # => true fake.main = 'true' fake.main? # => true fake.published = :true fake.published? # => true
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Don’t forget to rebase with branch master in main project before submit the pull request.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
(The MIT License)
Prodis a.k.a. Fernando Hamasaki de Amorim
Copyright © 2014-2015 Prodis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.