Module: TextDataTools::Named
- Defined in:
- lib/text-data-tools.rb
Overview
Tools for dealing with files where named variables are assigned in the form
name sep base
E.g.
height = 4.0
Defined Under Namespace
Classes: DataFile
Class Method Summary collapse
-
.get_variable_value(filename, name, sep = '=') ⇒ Object
Extract a variable value from the given file where the variable is defined in this form: name sep value E.g.
Class Method Details
.get_variable_value(filename, name, sep = '=') ⇒ Object
Extract a variable value from the given file where the variable is defined in this form:
name sep value
E.g. heat = 4.0
Both name and sep can be either regexps or strings
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/text-data-tools.rb', line 214 def self.get_variable_value(filename, name, sep='=') value = nil File.open(filename) do |file| while line= file.gets next unless line =~ Regexp.new("#{name.kind_of?(Regexp) ? name : Regexp.escape(name) }\\s*#{sep.kind_of?(Regexp) ? sep : Regexp.escape(sep) }\\s*(?<value>.*)") value = $~[:value] end end raise NotFoundError.new("Can't find #{name} in #{filename}") unless value value end |