Skip to content

Instantly share code, notes, and snippets.

@equivalent
Last active December 16, 2021 16:34

Revisions

  1. equivalent revised this gist Dec 16, 2021. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    This gist was writen in 2012 and it was solving specific problem in Rails & SimpleForm.
    Some fellow developers were pointing out this may be out dated concept.
    That's why I advise everyone to read comment section bellow to have a full grasp of alternative solutions

    other sources that may be helpful to understand why this may not be best idea:

    * [Why Ruby doesn’t have a Boolean class](https://www.rubytapas.com/2019/01/08/boolean/)
  2. equivalent renamed this gist Feb 19, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. equivalent revised this gist Feb 19, 2013. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # However there is also better practice to do it by using

    ActiveRecord::ConnectionAdapters::Column.value_to_boolean( something )

    2.0.0dev :019 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('1')
    => true
    2.0.0dev :020 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('0')
    => false
    2.0.0dev :021 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('true')
    => true
    2.0.0dev :022 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('false')
    => false
    2.0.0dev :023 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('nil')
    => false
    2.0.0dev :024 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('t')
    => true
    2.0.0dev :025 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean('cat')
    => false
    2.0.0dev :026 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(true)
    => true
    2.0.0dev :027 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(false)

    2.0.0dev :035 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(1)
    => true
    2.0.0dev :036 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(2)
    => false
    2.0.0dev :037 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(0)
    => false

  4. equivalent revised this gist Feb 19, 2013. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions string_boolean.rb
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,9 @@ def to_bool
    end
    class String; include StringToBoolean; end

    module Boolean
    module BooleanToBoolean
    def to_bool;return self; end
    end
    class TrueClass; include Boolean; end
    class FalseClass; include Boolean; end

    class TrueClass; include BooleanToBoolean; end
    class FalseClass; include BooleanToBoolean; end
  5. equivalent revised this gist Oct 3, 2012. No changes.
  6. equivalent revised this gist Oct 3, 2012. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions string_boolean.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    class String
    module StringToBoolean
    def to_bool
    return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
    return false if self == false || self.blank? || self =~ (/^(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
    end
    end
    end
    class String; include StringToBoolean; end

    module Boolean
    def to_bool;return self; end
    end
    class TrueClass; include Boolean; end
    class FalseClass; include Boolean; end
  7. equivalent revised this gist Oct 3, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion string_boolean.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    class String
    def to_bool
    return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
    return false if self == false || self.blank? || self =~ (^/(false|f|no|n|0)$/i)
    return false if self == false || self.blank? || self =~ (/^(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
    end
    end
  8. equivalent created this gist Oct 3, 2012.
    7 changes: 7 additions & 0 deletions string_boolean.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    class String
    def to_bool
    return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
    return false if self == false || self.blank? || self =~ (^/(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
    end
    end