Sha256: 5f40ad62ae8ec30cde05599729f7f1b72652b63c729a471884d1e86b9fae66dd

Contents?: true

Size: 1.74 KB

Versions: 24

Compression:

Stored size: 1.74 KB

Contents

class Object

  #
  # Default "integer?" behaviour.
  #
  def integer?; false; end

  #
  # Default "float?" behaviour.
  #
  def float?; false; end

  #
  # Default "number?" behaviour.
  #
  def number?; false; end

  #
  # `truthy?` means `not blank?`
  #
  def truthy?
    if respond_to? :blank?
      not blank?
    else
      not nil?
    end
  end

end


class TrueClass

  def truthy?; true; end

end


class FalseClass

  def truthy?; false; end

end


class Numeric

  def truthy?; self > 0; end

  def number?; true; end

end


class Integer

  #
  # 'true' if the integer is 0
  #
  def blank?; self == 0; end

  def integer?; true; end

end


class Float

  #
  # 'true' if the float is 0.0
  #
  def blank?; self == 0.0; end

  def float?; true; end

end


class NilClass

  #
  # Always 'true'; nil is considered blank.
  #
  def blank?; true; end

end


class Symbol

  #
  # Symbols are never blank.
  #
  def blank?; false; end

end


class String

  #
  # Could this string be cast to an integer?
  #
  def integer?
    !!strip.match(/^-?\d+$/)
  end

  #
  # Could this string be cast to an float?
  #
  def float?
    !!strip.match(/^-?\d+\.\d+$/)
  end

  #
  # Could this string be cast to an number?
  #
  def number?
    !!strip.match(/^-?\d\.?\d*$/)
  end

  #
  # 'true' if the string's length is 0 (after whitespace has been stripped from the ends)
  #
  def blank?
    strip.size == 0
  end

  #
  # Is there anything in the string? (ignoring whitespace/newlines)
  #
  def any?
    not blank?
  end
  alias_method :present?, :any?

  #
  # Does this string contain something that means roughly "true"?
  #
  def truthy?
    case strip.downcase
    when "1", "true", "yes", "on", "enabled", "affirmative"
      true
    else
      false
    end
  end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
epitools-0.5.131 lib/epitools/core_ext/truthiness.rb
epitools-0.5.130 lib/epitools/core_ext/truthiness.rb
epitools-0.5.129 lib/epitools/core_ext/truthiness.rb
epitools-0.5.128 lib/epitools/core_ext/truthiness.rb
epitools-0.5.126 lib/epitools/core_ext/truthiness.rb
epitools-0.5.125 lib/epitools/core_ext/truthiness.rb
epitools-0.5.124 lib/epitools/core_ext/truthiness.rb
epitools-0.5.123 lib/epitools/core_ext/truthiness.rb
epitools-0.5.122 lib/epitools/core_ext/truthiness.rb
epitools-0.5.121 lib/epitools/core_ext/truthiness.rb
epitools-0.5.119 lib/epitools/core_ext/truthiness.rb
epitools-0.5.118 lib/epitools/core_ext/truthiness.rb
epitools-0.5.116 lib/epitools/core_ext/truthiness.rb
epitools-0.5.115 lib/epitools/core_ext/truthiness.rb
epitools-0.5.114 lib/epitools/core_ext/truthiness.rb
epitools-0.5.113 lib/epitools/core_ext/truthiness.rb
epitools-0.5.112 lib/epitools/core_ext/truthiness.rb
epitools-0.5.111 lib/epitools/core_ext/truthiness.rb
epitools-0.5.110 lib/epitools/core_ext/truthiness.rb
epitools-0.5.109 lib/epitools/core_ext/truthiness.rb