If string, numeric, symbol and nil values wanna be a boolean value, they can with the new #to_b method (and more).
Ruby
Latest commit 0e5c82a Dec 26, 2016 @prodis Update Travis configuration.
Permalink
Failed to load latest commit information.
lib Bump to version 0.6.0. Aug 2, 2016
spec Invalid value behaviours (#6) Aug 2, 2016
.gitignore
.rspec
.travis.yml Update Travis configuration. Dec 26, 2016
CHANGELOG.md Bump to version 0.6.0. Aug 2, 2016
CONTRIBUTING.md Update documentation. Aug 2, 2016
Gemfile First commit. Oct 24, 2014
LICENSE Update documentation. Aug 2, 2016
README.md Update documentation. Aug 2, 2016
Rakefile First commit. Oct 24, 2014
wannabe_bool.gemspec

README.md

Wannabe Bool

If string, numeric, 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.

Gem Version Build Status Coverage Status Code Climate Dependency Status GitHub license

Installing

Gemfile

gem 'wannabe_bool'

Direct installation

$ gem install wannabe_bool

Using

to_b method is available on String, Symbol, Numeric, TrueClass, FalseClass and NilClass.

require 'wannabe_bool'

String

  • Returns true if string is one of these values: t, true, on, y, yes, 1.
  • Returns false if string is one of these values: f, false, off, n, no, 0.
  • For invalid boolean string representations, returns false by default. See "Invalid Value Behaviour" section for more options.

It ignores trailing spaces and letter cases.

'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

'0'.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

' 0 '.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
Invalid Value Behaviour

You can configure the result for invalid boolean string representations, using the WannabeBool.invalid_value_behaviour option.

There are 3 predefined behaviours available: to return false (default), nil or raise an ArgumentError:

# WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False
'wherever'.to_b # => false

WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Nil
'wherever'.to_b # => nil

WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Error
'wherever'.to_b # => ArgumentError: is not a valid boolean representation

Moreover you can provide your own behaviour for invalid boolean string representations. Just set a proc or lambda, or even any class or object that responds to call method.

WannabeBool.invalid_value_behaviour = -> { :prodis }
'wherever'.to_b # => :prodis

Note that WannabeBool.invalid_value_behaviour is a global configuration, so all results for to_b method with invalid boolean string representations will be affected.

Symbol

Same as symbol.to_s.to_b.

:'1'.to_b  # => true
:t.to_b    # => true
:true.to_b # => true
:on.to_b   # => true
:y.to_b    # => true
:yes.to_b  # => true

:'0'.to_b   # => false
:f.to_b     # => false
:false.to_b # => false
:off.to_b   # => false
:n.to_b     # => false
:no.to_b    # => false

Numeric

Returns false if number is zero. Returns true otherwise.

Integer
0.to_b  # => false
1.to_b  # => true
2.to_b  # => true
-1.to_b # => true
-2.to_b # => true
Float
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
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

TrueClass

Returns true.

true.to_b # => true

FalseClass

Returns false.

false.to_b # => false

NilClass

Returns 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

Changelog

See the changes in each version.

Author

Fernando Hamasaki de Amorim (prodis)

Prodis Logo

Contributing to wannabe_bool

See the contributing guide.