Sha256: 8be6ede6c2297d858d72c037fea040d02d9cd27d75a8d77b9627706709fe895f

Contents?: true

Size: 988 Bytes

Versions: 10

Compression:

Stored size: 988 Bytes

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('..')
      return Range.new(elements[0].to_i, elements[1].to_i)
    when 3
      elements = self.split('...')
      return Range.new(elements[0].to_i, elements[1].to_i-1)
    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
    print self
  end

  alias :/ :split
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.3.2 lib/darkext/string.rb
darkhelmet-darkext-0.4.0 lib/darkext/string.rb
darkhelmet-darkext-0.4.1 lib/darkext/string.rb
darkhelmet-darkext-0.4.2 lib/darkext/string.rb
darkhelmet-darkext-0.4.3 lib/darkext/string.rb
darkhelmet-darkext-0.5.0 lib/darkext/string.rb
darkhelmet-darkext-0.5.1 lib/darkext/string.rb
darkhelmet-darkext-0.5.2 lib/darkext/string.rb
darkhelmet-darkext-0.5.3 lib/darkext/string.rb
darkhelmet-darkext-0.5.4 lib/darkext/string.rb