BooleanClass
Warning
Looking for typecasting boolean values?
Just use the "bang bang" technique, there is no need for an external gem to achieve that.
!!(0.0) #=> true
!!1 #=> true
!!0 #=> true
!!"" #=> true
!!nil #=> false
!!false #=> false
Matz, the creator of Ruby, has a strong opinion against a Boolean class:
There's nothing true and false commonly share, thus no Boolean class. Besides that, in Ruby, everything behave as Boolean value, as Xavier mentioned in [ruby-talk:406208]. matz.
That said, this is just a proof-of-concept gem for some of my experiments.
Description
Performs type conversion from anything to true:TrueClass / false:FalseClass
Boolean(0.0) #=> true
FalseClass < Boolean #=> true
Anti-Features
Similar to boolean gem but:
- Without parse_bool()
- Without raising exceptions
- Without to_bool() to_b() methods
- Does not pollute Object or Kernel; polite monkey patching with
include BooleanClass::Conversion
Installation
$ gem install boolean_class
or add to your Gemfile this line: gem 'boolean_class'
then run bundle install
Usage
Just require 'boolean_class'
and then use it as:
As a conversion method Boolean()
require 'boolean_class'
# Example true values
Boolean(true) #=> true
Boolean(999) #=> true
Boolean(0) #=> true
# Example false values
Boolean(false) #=> false
Boolean(nil) #=> false
As data type checking / validation
require 'boolean_class'
# Instance type checking
true.is_a?(Boolean) #=> true
false.is_a?(Boolean) #=> true
0.is_a?(Boolean) #=> false
nil.is_a?(Boolean) #=> false
(0==1).is_a?(Boolean) #=> true
# Class type checking
Object < Boolean #=> nil (false)
NilClass < Boolean #=> nil (false)
Class < Boolean #=> nil (false)
TrueClass < Boolean #=> true
FalseClass < Boolean #=> true
Contributing
- Fork it.
- Make your feature addition or bug fix and create your feature branch.
- Update the Change Log.
- Add specs/tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, create a new Pull Request.
- Check that your pull request passes the build.
License
Released under the MIT License. See the LICENSE file for further details.
Links
RubyGems | Documentation | Source | Bugtracker | Build Status | Dependency Status | Code Climate