Last active
September 9, 2018 12:11
-
-
Save shcyiza/8c0ccd9d41bbe4bb9d5db33002be2b0c to your computer and use it in GitHub Desktop.
variable scanner for hapi_mailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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