Sha256: 417096780bb27f0346a9a862972fb242d033994dc24896b3a2fb44bb42860436

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

module Kernel

  # Tests to see if something has value. An object
  # is considered to have value if it is not nil?
  # and if it responds to #empty?, is not empty.
  #
  def val?
    return false if nil?
    return false if empty? if respond_to?(:empty?)
    true
  end

  # The opposite of #nil?.
  #
  #   "hello".not_nil?     # -> true
  #   nil.not_nil?         # -> false
  #
  #   CREDIT Gavin Sinclair

  def not_nil?
    not nil?
  end

  # TODO Do not need two of these.
  #alias_method :non_nil?, :not_nil?

  # Is self included in other?
  #
  #   5.in?(0..10)       #=> true
  #   5.in?([0,1,2,3])   #=> false
  #
  def in?(other)
    other.include?(self)
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TestKernelVal < Test::Unit::TestCase

    def test_val_1
      f = nil
      t = 1
      assert( ! f.val? )
      assert( t.val? )
    end

    def test_val_2
      f = []
      t = [1]
      assert( ! f.val? )
      assert( t.val? )
    end

    def test_val_3
      f = ''
      t = '1'
      assert( ! f.val? )
      assert( t.val? )
    end

    def test_in?
      assert( 5.in?(0..10) )
      assert( 5.in?([1,2,3,4,5]) )
    end

  end

=end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facets-2.1.0 lib/core/facets/kernel/val.rb
facets-2.0.5 lib/core/facets/kernel/val.rb
facets-2.0.3 lib/core/facets/kernel/val.rb
facets-2.0.4 lib/core/facets/kernel/val.rb
facets-2.1.1 lib/core/facets/kernel/val.rb
facets-2.1.2 lib/core/facets/kernel/val.rb
facets-2.1.3 lib/core/facets/kernel/val.rb