ruby on rails - regular expression a string -
i need regular expression convert following object:
"{:email=>[\"has been taken\"], :name=>[\"has been taken\"]}"
to
email has been taken, name has been taken
▶ str = "{:email=>[\"has been taken\"], :name=>[\"has been taken\"]}" ▶ eval(str).map { |k, v| [k.capitalize, v].join(' ') }.join(', ') #⇒ "email has been taken, name has been taken"
with scan
:
▶ str.scan(/:(\w+)=>\["(.*?)"\]/).map |k, v| ▷ [k.capitalize, v].join(' ') ▷ end.join(', ') #⇒ "email has been taken, name has been taken"
Comments
Post a Comment