How to render a string as an erb file?

2019-02-10 04:09发布

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?

1条回答
可以哭但决不认输i
2楼-- · 2019-02-10 04:51

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"
查看更多
登录 后发表回答