使用OpenStruct与雇员再培训局的问题 [英] Problem using OpenStruct with ERB

查看:190
本文介绍了使用OpenStruct与雇员再培训局的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:忘记了,包括我的环境信息... Win7x64,RubyInstaller红宝石v1.9.1的-P378

forgot to include my environment info... Win7x64, RubyInstaller Ruby v1.9.1-p378

编辑2:刚刚更新至v1.9.1的,429打补丁,但还是收到此相同的错误。

EDIT 2: just updated to v1.9.1, patch 429, and still getting this same error.

修改3:运行在Ruby中v1.8.7同​​一code,249打补丁,做工精细。所以这是v1.9.1的是打破了它,显然。

Edit 3: running this same code in Ruby v1.8.7, patch 249, works fine. so it's v1.9.1 that broke it, apparently.

我是新来使用再培训局,我能找到的样本......嗯......不到有用的...有与雇员再培训局四处打了大约一个小时,我的工作(最终)的一些基本的例子,但我不知道为什么这不工作...

I'm new to using ERB and the samples i could find are... ummm... less than helpful... having played around with ERB for about an hour, I got some basic examples working (finally), but I have no idea why this doesn't work...

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)

这code产生以下错误:

this code produces the following error:

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 `'

为什么会在看主:对象绑定?我告诉它在 vars_binding

有人可以填补我为什么它不工作,并帮助我得到它的工作?

can someone fill me in on why it doesn't work, and help me get it to work?

推荐答案

我遇到与红宝石1.9.2类似code相同类型的错误时,偶然发现了这个问题。

Fix to Problem:

I stumbled upon this question when encountering the same type of error with similar code in Ruby 1.9.2.

我是新来的Ruby,所以我无法解释发生了什么。我继续在网上搜索,发现这个博客帖子有这似乎工作的方法。修改你的榜样将这一做法我结束了以下,工作后,code:

I'm new to Ruby so I can't explain what is happening. I continued to search online and found this blog post that has an approach that seems to work. After modifying your example to incorporate this approach I end up with the following, working, code:

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)


其他信息:

<子>当code运行直通的IRB,我得到:

When the code is run thru the IRB, I get:

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

这篇关于使用OpenStruct与雇员再培训局的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆