Skip to content

Instantly share code, notes, and snippets.

@shcyiza
Last active September 9, 2018 12:11
Show Gist options
  • Save shcyiza/8c0ccd9d41bbe4bb9d5db33002be2b0c to your computer and use it in GitHub Desktop.
Save shcyiza/8c0ccd9d41bbe4bb9d5db33002be2b0c to your computer and use it in GitHub Desktop.
variable scanner for hapi_mailer
str = "<container>\n<row>\n <columns large=\"6\">\n <h1>%{toto}%</h1>\n </columns>\n %{kilo}%\n <columns large=\"6\">\n <i class=\"classx\">%{socco}%</i>\n </columns>\n %{kilo}%\n</row>\n</container>"
# /%{+\w+}%/ is the regex of the chosen string interpolation synthax e.g. %{variable}%
# we try to extract all of those and put them in an array
# first lets get the all the interpolation out of the text
interpolition_arr = str.scan(/%{+\w+}%/)
# => ["%{toto}%", "%{kilo}%", "%{socco}%", "%{kilo}%"]
# make each variable have only one occurence in the variable array
variable_arr = []
interpolition_arr.map{|w| variable_arr.include?(w) == false ? variable_arr << w : nil }
# now viarable_arr returns ["%{toto}%", "%{kilo}%", "%{socco}%"]
# finally get the variable name without the interpolation synhtax
variable_arr.map{|i| i.gsub(/[%{} ]/, '' )}
# => ["toto", "kilo", "socco"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment