1

从 Ruby 脚本中,有没有办法实用地获取在 erubis 文件中找到的所有预期变量名和函数名的列表?

例如,如何从 Eruby 对象中确定包含变量“名称”?

require 'erubis'
source = 'Hello, <%= name %>'
erb = Erubis::Eruby.new(source)
4

1 回答 1

0

我的骇人听闻的解决方案:

# from the original question
require 'erubis'
source = 'Hello, <%= name %>'
erb = Erubis::Eruby.new(source)

# a not so great solution
vars = {}
begin
  Erubis::Eruby.new(code).result(vars)
rescue NameError => e
  puts "Found: '#{e.name}'"
  vars[e.name.to_sym] = nil
  retry
end

结果是:

Found: 'name'
于 2013-02-08T15:09:14.467 回答