Sha256: 3d51adba23310d895cad58eb6f79df293b3b854ef6ff9c16cfc86c90c1d48383

Contents?: true

Size: 1.92 KB

Versions: 37

Compression:

Stored size: 1.92 KB

Contents

class Regexp
  NUMERIC = Regexp.union(%r{^-?\d{1,3}(,\d{3}|\d+)*(\.\d+)*$},
    %r{^\(?\d{1,3}(,\d{3}|\d+)*(\.\d+)*\)?$})
end

class String
  def to_boolean
    return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
    return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
  end

  # Like #index but returns a Range.
  #
  #   "This is a test!".range('test')  #=> (10..13)
  #
  # CREDIT: Trans

  def range(pattern, offset=0)
    unless Regexp === pattern
      pattern = Regexp.new(Regexp.escape(pattern.to_s))
    end
    string = self[offset..-1]
    if md = pattern.match(string)
      return (md.begin(0)+offset)..(md.end(0)+offset-1)
    end
    nil
  end

  # Like #index_all but returns an array of Ranges.
  #
  #   "abc123abc123".range_all('abc')  #=> [0..2, 6..8]
  #
  # TODO: Add offset ?
  #
  # CREDIT: Trans

  def range_all(pattern, reuse=false)
    r = []; i = 0
    while i < self.length
      rng = range(pattern, i)
      if rng
        r << rng
        i += reuse ? 1 : rng.end + 1
      else
        break
      end
    end
    r.uniq
  end

  # Returns an array of ranges mapping
  # the characters per line.
  #
  #   "this\nis\na\ntest".range_of_line
  #   #=> [0..4, 5..7, 8..9, 10..13]
  #
  # CREDIT: Trans

  def range_of_line
    offset=0; charmap = []
    each_line do |line|
      charmap << (offset..(offset + line.length - 1))
      offset += line.length
    end
    charmap
  end

  def numeric?
    match(Regexp::NUMERIC) != nil
  end

  def integer?
    begin
      Integer(self)
      self
    rescue ArgumentError => e
      nil
    end
  end

  def date?
    begin
      d = Date.parse(self)
    rescue ArgumentError => e
      nil
    end
  end

  def ssn?
    if  (integer? && length == 9)
      self
    elsif ((self =~ /[0-9]{3}-[0-9]{2}-[0-9]{4}/ ) == 0)
      self.gsub('-','')
    else
      nil
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
abstractor-2.0.1 lib/abstractor/core_ext/string.rb
abstractor-2.0.0 lib/abstractor/core_ext/string.rb
abstractor-1.0.23 lib/abstractor/core_ext/string.rb
abstractor-1.0.22 lib/abstractor/core_ext/string.rb
abstractor-1.0.20 lib/abstractor/core_ext/string.rb
abstractor-1.0.19 lib/abstractor/core_ext/string.rb
abstractor-1.0.18 lib/abstractor/core_ext/string.rb
abstractor-1.0.17 lib/abstractor/core_ext/string.rb
abstractor-1.0.16 lib/abstractor/core_ext/string.rb
abstractor-1.0.15 lib/abstractor/core_ext/string.rb
abstractor-1.0.14 lib/abstractor/core_ext/string.rb
abstractor-1.0.13 lib/abstractor/core_ext/string.rb
abstractor-1.0.12 lib/abstractor/core_ext/string.rb
abstractor-1.0.11 lib/abstractor/core_ext/string.rb
abstractor-1.0.10 lib/abstractor/core_ext/string.rb
abstractor-1.0.9 lib/abstractor/core_ext/string.rb
abstractor-1.0.8 lib/abstractor/core_ext/string.rb