Sha256: 63298e73a85345cc2efc8239038f8ac072ffc640903ea5c3765c5f202b9b69f2

Contents?: true

Size: 892 Bytes

Versions: 6

Compression:

Stored size: 892 Bytes

Contents

module DR
  module Bool
    extend(self)
    def to_bool(el, default=nil, allow_nil: true, string_fallback: true)
      case el
      when String
        string=el.chomp
        return true if string =~ (/(true|t|yes|y|1)$/i)
        return false if string.empty? || string =~ (/(false|f|no|n|0)$/i)
        return el if string_fallback
      when Integer
        return ! (el == 0)
      when ::Process::Status
        exitstatus=el.exitstatus
        return exitstatus == 0
      else
        return true if el == true
        return false if el == false
        #we don't return !!el because we don't want nil to be false but to
        #give an error
      end
      return el if string_fallback and el.is_a?(Symbol)
      return default unless default.nil?
      return nil if el.nil? and allow_nil
      raise ArgumentError.new("Invalid value for Boolean: \"#{el}\"")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
drain-0.7.0 lib/dr/base/bool.rb
drain-0.6.0 lib/dr/base/bool.rb
drain-0.5.1 lib/dr/base/bool.rb
drain-0.5 lib/dr/base/bool.rb
drain-0.4 lib/dr/base/bool.rb
drain-0.3.0 lib/dr/base/bool.rb