Sha256: 917ddadb4162b6b1bf8528d24482eef35b669c30d5b53bb75784fe781bba8a7a

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 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] - 1).to_i)
      else
        return Range.new(elements[0], elements[1] - 1)
      end
    end
    return nil
  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)
    cmd = self
    # TODO: Do this with threads maybe, so it's OS agnostic?
    cmd += " &" if opts[:background] && !opts[:capture]
    return system(cmd) if !opts[:capture]
    return `#{cmd}` if opts[:capture]
  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

  alias :/ :split
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.9.2 lib/darkext/string.rb
darkhelmet-darkext-0.9.3 lib/darkext/string.rb
darkhelmet-darkext-0.9.4 lib/darkext/string.rb