Sha256: 36d9584dca6c550db1933a8f277d2651219e20dc9d1706aa0525a4c260b7e770

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'darkext/hash'

class String
  # Parses a string like "1..10" to a Range
  def to_range
    case self.count('.')
    when 2
      elements = self.split('..')
      if elements[0] == elements[0].to_i.to_s
        return Range.new(elements[0].to_i, elements[1].to_i)
      else
        return Range.new(elements[0], elements[1])
      end
    when 3
      elements = self.split('...')
      if elements[0] == elements[0].to_i.to_s
        return Range.new(elements[0].to_i, elements[1].to_i, true)
      else
        return Range.new(elements[0], elements[1], true)
      end
    end
    raise ArgumentError.new('Could not parse range')
  end

  # Executes the string with system
  # * :background => true to run command in the background using & (currently only works on *nix systems)
  # * :capture => true to capture the output. If :capture => true, background is voided
  def exec(opts = {})
    opts.with_defaults!(:background => false, :capture => false)
    return `#{self}` if opts[:capture]
    return fork { system(self) } if opts[:background]
    return system(self)
  end

  # Prints the String using print
  def print
    Kernel.print(self)
  end

  def printn
    Kernel.print(self + "\n")
  end

  def true?
    self.downcase == 'true'
  end

  def false?
    self.downcase == 'false'
  end

  def starts_with?(str)
    str = str.to_s
    str == self[0, str.length]
  end

  def ends_with?(str)
    str = str.to_s
    str == self[-str.length, str.length]
  end

  alias :/ :split
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
darkhelmet-darkext-0.11.1 lib/darkext/string.rb
darkhelmet-darkext-0.11.2 lib/darkext/string.rb
darkhelmet-darkext-0.12.0 lib/darkext/string.rb
darkext-0.12.0 lib/darkext/string.rb