使用OpenBtruct和ERB时出现问题

时间:2010-07-14 00:04:54

标签: ruby binding erb nameerror

编辑:忘了包含我的环境信息... Win7x64,RubyInstaller Ruby v1.9.1-p378

编辑2:刚更新到v1.9.1,修补程序429,仍然遇到同样的错误。

编辑3:在Ruby v1.8.7,补丁249中运行相同的代码,运行正常。所以它的v1.9.1显然打破了它。

我是新手使用ERB,我可以找到的样本......嗯......不太有帮助......在ERB玩了大约一个小时后,我得到了一些基本的例子(最后),但我不知道为什么这不起作用......

require 'ostruct'
require 'erb'

data = {:bar => "bar"}
vars = OpenStruct.new(data)

template = "foo "
erb = ERB.new(template)

vars_binding = vars.send(:binding)
puts erb.result(vars_binding)

此代码产生以下错误:

irb(main):007:0> puts erb.result(vars_binding)
NameError: undefined local variable or method `bar' for main:Object
        from (erb):1
        from C:/Ruby/v1.9.1/lib/ruby/1.9.1/erb.rb:753:in `eval'
        from C:/Ruby/v1.9.1/lib/ruby/1.9.1/erb.rb:753:in `result'
        from (irb):7
        from C:/Ruby/v1.9.1/bin/irb:12:in `'

为什么要关注main:Object绑定?我告诉它通过传递vars_binding

来使用OpenStruct中的绑定

有人可以告诉我为什么它不起作用,并帮助我让它工作?

4 个答案:

答案 0 :(得分:19)

问题在于执行绑定的位置。 1.8.7-way obj.send(:binding)不再起作用(参见issue2161),环境必须是对象本身。所以使用instance_eval

require 'ostruct'
require 'erb'

namespace = OpenStruct.new(:first => 'Salvador', :last => 'Espriu')
template = 'Name: <%= first %> <%= last %>'
ERB.new(template).result(namespace.instance_eval { binding })
#=> Name: Salvador Espriu

有关此问题的更多信息in this answer

答案 1 :(得分:4)

修复问题:

当遇到Ruby 1.9.2中类似代码的相同类型的错误时,我偶然发现了这个问题。

我是Ruby的新手,所以我无法解释发生了什么。我继续在网上搜索,发现this blog post有一种似乎有用的方法。在修改您的示例以合并此方法后,我最终得到以下工作代码:

require 'ostruct'
require 'erb'

class ErbBinding < OpenStruct
    def get_binding
        return binding()
    end
end

data = {:bar => "baz"}
vars = ErbBinding.new(data)

template = "foo <%= bar %>"
erb = ERB.new(template)

vars_binding = vars.send(:get_binding)
puts erb.result(vars_binding)

其他信息:

当代码通过IRB运行时,我得到:

require 'ostruct'
=> true
require 'erb'
=> true

class ErbBinding < OpenStruct
    def get_binding
        return binding()
    end
end
=> nil

data = {:bar => "baz"}
=> {:bar=>"baz"}
vars = ErbBinding.new(data)
=> #<ErbBinding bar="baz">

template = "foo <%= bar %>"
=> "foo <%= bar %>"
erb = ERB.new(template)
=> #<ERB:0x2b73370 @safe_level=nil, @src="#coding:IBM437\n_erbout = ''; _erbout.concat \"foo \"; _erbout.concat(( bar ).to_s); _erbout.force_encoding(__ENCODING__)", @enc=#<Encoding:IBM437>, @filename=nil>

vars_binding = vars.send(:get_binding)
=> #<Binding:0x2b6d418>
puts erb.result(vars_binding)
foo baz
=> nil

答案 2 :(得分:2)

您的环境是什么样的?这段代码对我有用(我只是将字符串“bar”更改为“baz”以消除我脑中的歧义,并将其添加到模板中):

require 'ostruct'
require 'erb'

data = {:bar => "baz"}
vars = OpenStruct.new(data)

template = "foo <%= bar %>"
erb = ERB.new(template)

vars_binding = vars.send(:binding)
puts erb.result(vars_binding)

当我运行它时,我得到:

defeateds-MacBook-Pro:Desktop defeated$ ruby erb.rb 
foo baz

OSX下的1.8.7:

defeateds-MacBook-Pro:Desktop defeated$ ruby -v
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

答案 3 :(得分:0)

看起来这不适用于更高级的ruby版本

使用ruby 2.1.1

[19] pry(main)> name = "samtoddler"
=> "Suresh"
[20] pry(main)> template_string = "My name is <%= name %>"
=> "My name is <%= name %>"
[21] pry(main)> template = ERB.new template_string
=> #<ERB:0x007fadf3491c38
 @enc=#<Encoding:UTF-8>,
 @filename=nil,
 @safe_level=nil,
 @src="#coding:UTF-8\n_erbout = ''; _erbout.concat \"My name is \"; _erbout.concat(( name ).to_s); _erbout.force_encoding(__ENCODING__)">
[22] pry(main)> puts template.result
NameError: undefined local variable or method `name' for main:Object
from (erb):1:in `<main>'

使用ruby 1.9.3

[2] pry(main)> name = "samtoddler"
=> "Suresh"
[3] pry(main)> template_string = "My name is <%= name %>"
=> "My name is <%= name %>"
[4] pry(main)> template = ERB.new template_string
=> #<ERB:0x007f9be2a1fdf8
 @enc=#<Encoding:UTF-8>,
 @filename=nil,
 @safe_level=nil,
 @src=
  "#coding:UTF-8\n_erbout = ''; _erbout.concat \"My name is \"; _erbout.concat(( name ).to_s); _erbout.force_encoding(__ENCODING__)">
[5] pry(main)> puts template.result
My name is samtoddler

因此它会出错,但仍然适用于1.9.3以及1.9.3以下的所有版本。