Module: Utilities

Defined in:
lib/utilities.rb

Instance Method Summary collapse

Instance Method Details

#read_conf_file(filepath) ⇒ Object

Reads files with K-V pairs in the style of KEY=value KEY2=“really long value” and strips surrounding spaces, then surrounding double-quotes, then returns K-V pairs as a hash



7
8
9
10
11
12
13
14
# File 'lib/utilities.rb', line 7

def read_conf_file(filepath)
  text = File.read(filepath)
  data = text.scan(/(\S+)\s*=\s*(.+)/)
  hash = Hash[data]
  return hash.map { |k, v| [k, v.strip.delete_prefix('"').delete_suffix('"')] }.to_h
  # TODO: Support comments (starting with #). Possibly non-leading # (such as after value, but outside quotes.)
  # TODO: Handle quotes more elegantly. State machine?
end