Sha256: ada4c4ef89677734f3e6f5480b9d2d1e8211e286edd9f6302d3291d26199fd0c

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

class String

  ##
  # Extending String function to be able to convert a String 'true' or 'false' to a boolean true/false.
  # See the Boolean module for usage
  #
  def to_bool
    (self =~ /^true$/i) == 0
  end

end

class TrueClass

  ##
  # Implementing to_bool operation that returns self.
  # See the Boolean module for usage
  #
  def to_bool
    self
  end

end

class FalseClass

  ##
  # Implementing to_bool operation that returns self.
  # See the Boolean module for usage
  #
  def to_bool
    self
  end

end

class NilClass

  ##
  # Implementing to_bool operation that returns self.
  # See the Boolean module for usage
  #
  def to_bool
    self
  end

end

module Toolbox

  ##
  # The main application of the Boolean module is to
  # support reading boolean values from a String (e.g.
  # while reading a configuration value) and having the
  # ability to convert it back to a boolean true/false
  # for easier evaluation in your Ruby code
  #
  # == Usage
  #
  # Working with Boolean module can be very simple, for example:
  #
  #     require 'toolbox/boolean'
  #
  #     list_of_values = [
  #       'true',
  #       'True',
  #       'TRUE',
  #       'false',
  #       'False',
  #       'FALSE',
  #       nil,
  #     ]
  #
  #     list_of_values.each do |string|
  #       puts "This evaluated to true" if string.to_bool
  #       puts "This evaluated to false or was nil" unless string.to_bool
  #     end
  #
  module Boolean
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-development-toolbox-1.4.0 lib/toolbox/boolean.rb
ruby-development-toolbox-1.3.2 lib/toolbox/boolean.rb