All Projects → prodis → Wannabe_bool

prodis / Wannabe_bool

Licence: mit
If string, numeric, symbol and nil values wanna be a boolean value, they can with the new #to_b method (and more).

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Wannabe bool

Timezone Support
Lightweight time zone support for your applications or other date libraries.
Stars: ✭ 90 (-42.31%)
Mutual labels:  conversion
Calibre Kobo Driver
An extension of the existing KoboTouch driver provided with Calibre. This plugin allows modifying ePub files to enable extra Kobo features. I am providing code in the repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not my employer.
Stars: ✭ 129 (-17.31%)
Mutual labels:  conversion
Effectsize
🐉 Compute and work with indices of effect size and standardized parameters
Stars: ✭ 146 (-6.41%)
Mutual labels:  conversion
Ec2 Spot Converter
A tool to convert AWS EC2 instances back and forth between On-Demand and Spot billing models.
Stars: ✭ 108 (-30.77%)
Mutual labels:  conversion
Alltomp3
Node module to download and convert in MP3 with tags an online video
Stars: ✭ 120 (-23.08%)
Mutual labels:  conversion
Hrconvert2
A self-hosted, drag-and-drop, & nosql file conversion server that supports 62x file formats.
Stars: ✭ 132 (-15.38%)
Mutual labels:  conversion
Batch textures convert
🔁 Batch texture conversion to various render-friendly mip-mapped formats
Stars: ✭ 67 (-57.05%)
Mutual labels:  conversion
Pyreadstat
Python package to read sas, spss and stata files into pandas data frames. It is a wrapper for the C library readstat.
Stars: ✭ 151 (-3.21%)
Mutual labels:  conversion
Asm2c
Tool to convert DOS Assembly code to C code
Stars: ✭ 121 (-22.44%)
Mutual labels:  conversion
Easyrs
Convenience RenderScript tools for processing common Android formats such as Bitmap and NV21.
Stars: ✭ 144 (-7.69%)
Mutual labels:  conversion
Nb pdf template
A more accurate representation of jupyter notebooks when converting to pdfs.
Stars: ✭ 109 (-30.13%)
Mutual labels:  conversion
Dataclass factory
Modern way to convert python dataclasses or other objects to and from more common types like dicts or json-like structures
Stars: ✭ 116 (-25.64%)
Mutual labels:  conversion
Typestat
Converts JavaScript to TypeScript and TypeScript to better TypeScript.
Stars: ✭ 136 (-12.82%)
Mutual labels:  conversion
Snakecase
🐍🐍🐍 A systematic approach to parse strings and automate the conversion to snake_case, UpperCamelCase or any other case.
Stars: ✭ 104 (-33.33%)
Mutual labels:  conversion
Figmatocode
Generate responsive pages and apps on HTML, Tailwind, Flutter and SwiftUI.
Stars: ✭ 2,299 (+1373.72%)
Mutual labels:  conversion
Unit
Conversion of unit library for golang
Stars: ✭ 80 (-48.72%)
Mutual labels:  conversion
Exchanger
🏢 Currency exchange rates framework for PHP
Stars: ✭ 133 (-14.74%)
Mutual labels:  conversion
Convertio Php
Convertio APIs Client Library for PHP
Stars: ✭ 153 (-1.92%)
Mutual labels:  conversion
Sv2v
SystemVerilog to Verilog conversion
Stars: ✭ 151 (-3.21%)
Mutual labels:  conversion
Swiftrewriter
A Swift Package Manager console app and library to convert Objective-C code into Swift.
Stars: ✭ 140 (-10.26%)
Mutual labels:  conversion

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.

For sake of readability (and personal choice), to_b has two aliases:

  • to_bool
  • to_boolean

Given this example:

{
  one: 'value',
  two: 2,
  mobile?: params[:mobile].to_b
}

It could be "more readable" like this:

{
  one: 'value',
  two: 2,
  mobile?: params[:mobile].to_boolean
}

Don't forget to require the gem:

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

''.to_b  # => false
' '.to_b # => false

Invalid Value Behaviour for strings

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. Said that, all the 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].