Sha256: 65f3b19b29ab8e7cf236f0c22f8fe97c01042922acb0e4dd2fc1ecd2687c1ffc

Contents?: true

Size: 1.75 KB

Versions: 21

Compression:

Stored size: 1.75 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

21 entries across 21 versions & 1 rubygems

Version Path
epitools-0.5.103 lib/epitools/core_ext/truthiness.rb
epitools-0.5.100 lib/epitools/core_ext/truthiness.rb
epitools-0.5.99 lib/epitools/core_ext/truthiness.rb
epitools-0.5.98 lib/epitools/core_ext/truthiness.rb
epitools-0.5.97 lib/epitools/core_ext/truthiness.rb
epitools-0.5.96 lib/epitools/core_ext/truthiness.rb
epitools-0.5.95 lib/epitools/core_ext/truthiness.rb
epitools-0.5.94 lib/epitools/core_ext/truthiness.rb
epitools-0.5.93 lib/epitools/core_ext/truthiness.rb
epitools-0.5.92 lib/epitools/core_ext/truthiness.rb
epitools-0.5.91 lib/epitools/core_ext/truthiness.rb
epitools-0.5.90 lib/epitools/core_ext/truthiness.rb
epitools-0.5.89 lib/epitools/core_ext/truthiness.rb
epitools-0.5.88 lib/epitools/core_ext/truthiness.rb
epitools-0.5.87 lib/epitools/core_ext/truthiness.rb
epitools-0.5.86 lib/epitools/core_ext/truthiness.rb
epitools-0.5.85 lib/epitools/core_ext/truthiness.rb
epitools-0.5.84 lib/epitools/core_ext/truthiness.rb
epitools-0.5.83 lib/epitools/core_ext/truthiness.rb
epitools-0.5.82 lib/epitools/core_ext/truthiness.rb