Sha256: ca96302d6d55a3e38f0b33fd1633a20f5047cebcf9c173e1188da45b8c9dfcfd

Contents?: true

Size: 645 Bytes

Versions: 4

Compression:

Stored size: 645 Bytes

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:
  #
  #   require 'facet/string/to_b'
  #
  #   "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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/string/to_b.rb
facets-0.7.0 lib/facet/string/to_b.rb
facets-0.7.1 lib/facet/string/to_b.rb
facets-0.7.2 lib/facet/string/to_b.rb