Sha256: 0f57a2dacee8ccb6b0f17cf74abdcf6a772a90e79dcc621087dd5fbdaa9ff6c5

Contents?: true

Size: 1.27 KB

Versions: 26

Compression:

Stored size: 1.27 KB

Contents

class String

  # Interpret common affirmative string meanings as true,
  # otherwise false. Balnk sapce and case are ignored.
  # The following strings that will return true:
  #
  #   <tt>true</tt>,<tt>yes</tt>,<tt>on</tt>,<tt>t</tt>,<tt>1</tt>,<tt>y</tt>,<tt>==</tt>
  #
  # Examples:
  #
  #   "true".to_b   #=> true
  #   "yes".to_b    #=> true
  #   "no".to_b     #=> false
  #   "123".to_b    #=> false
  #
  def to_b
    case self.downcase.strip
    when 'true', 'yes', 'on', 't', '1', 'y', '=='
      return true
    when 'nil', 'null'
      return nil
    else
      return false
    end
  end

end


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

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_to_b
      assert( 'true'.to_b )
      assert( 'True'.to_b )
      assert( 'yes'.to_b )
      assert( 'YES'.to_b )
      assert( 'on'.to_b )
      assert( 'ON'.to_b )
      assert( 't'.to_b )
      assert( '1'.to_b )
      assert( 'y'.to_b )
      assert( 'Y'.to_b )
      assert( '=='.to_b )
      assert( ! 'nil'.to_b )
      assert( ! 'false'.to_b )
      assert( ! 'blahblahtrueblah'.to_b )
      assert_equal( nil, 'nil'.to_b )
      assert_equal( nil, 'null'.to_b )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/string/to_b.rb
facets-1.0.0 lib/facet/string/to_b.rb
facets-0.9.0 lib/nano/string/to_b.rb
facets-1.2.1 lib/facets/core/string/to_b.rb
facets-1.1.0 lib/facet/string/to_b.rb
facets-1.2.0 lib/facets/core/string/to_b.rb
facets-1.3.0 lib/facets/core/string/to_b.rb
facets-1.3.3 lib/facets/core/string/to_b.rb
facets-1.3.2 lib/facets/core/string/to_b.rb
facets-1.3.1 lib/facets/core/string/to_b.rb
facets-1.4.0 lib/facets/core/string/to_b.rb
facets-1.4.2 lib/facets/core/string/to_b.rb
facets-1.4.1 lib/facets/core/string/to_b.rb
facets-1.4.3 lib/facets/core/string/to_b.rb
facets-1.4.5 lib/facets/core/string/to_b.rb
facets-1.4.4 lib/facets/core/string/to_b.rb
facets-1.7.0 lib/facets/core/string/to_b.rb
facets-1.7.38 lib/facets/core/string/to_b.rb
facets-1.7.30 lib/facets/core/string/to_b.rb
facets-1.7.46 lib/facets/core/string/to_b.rb