Created
November 16, 2021 20:25
-
-
Save emptyflask/fa6fd97c827f7655308dfa6f482a5ecc to your computer and use it in GitHub Desktop.
ruby `#to_boolean` extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bigdecimal' | |
module CoreExtensions | |
module String | |
def to_boolean | |
!!(self =~ /^(true|t|yes|y|1)$/i) | |
end | |
end | |
module Integer | |
def to_boolean | |
if self == 1 | |
true | |
else | |
false | |
end | |
end | |
end | |
module BigDecimal | |
def to_boolean | |
# BigDecimal#== automatically coerces values | |
if self == 1 | |
true | |
else | |
false | |
end | |
end | |
end | |
module NilClass | |
def to_boolean | |
false | |
end | |
end | |
module TrueClass | |
def to_i; 1; end | |
def to_boolean | |
self | |
end | |
end | |
module FalseClass | |
def to_i; 0; end | |
def to_boolean | |
self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment