Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a string as an erb file?

How can I render a string like erb files render.

for example I want this string:

"Hello <%= 'World'%>"

To be:

"Hello World"

How can I do this?

like image 966
Oded Harth Avatar asked Nov 01 '11 14:11

Oded Harth


1 Answers

If I properly understand you, this would be helpful:

require 'erb'
str = "Hello <%= 'World'%>"
result = ERB.new(str).result  # => "Hello World"

UPDATE

If you want to use variables:

require 'erb'
w = "World"
str = "Hello <%= w %>"
result = ERB.new(str).result(binding)  # => "Hello World"
like image 94
WarHog Avatar answered Oct 29 '22 00:10

WarHog