Sha256: 00ab58ebd6b801e520efb159ac783540d1bd8b6878bdd1646088de74702cad5b
Contents?: true
Size: 1 KB
Versions: 5
Compression:
Stored size: 1 KB
Contents
# Expands variables in a template string. Variables are prefixed with a $ sign # and consist of word characters (letters, digits, underscores). The name can # be enclosed in curly braces to avoid unintended expansions. The $ sign can be # escaped with a backslash (\), and backslashes can be escaped with another # backslash. All occurrences of a variable in the string are replaced # # The str argument is the template string and the variables argument is a hash # from variable name (Symbol) to variable value # # Note that the characters '\x00' and '\x01' are used internally and may not be # present in the template string # def expand_variables(str, variables) # ChatGPT # Replace escaped bashslashes and dollar signs str = str.gsub('\\\\', "\x00").gsub('\\$', "\x01") # Expand variables str.gsub!(/\$(\w+)\b|\$\{(\w+)\}/) do |match| # Strange that '\b' is necessary key = ($1 || $2).to_sym variables[key] || match end # Restore escaped characters str.gsub("\x00", '\\').gsub("\x01", '$') end
Version data entries
5 entries across 5 versions & 1 rubygems