使用带有 ERB 的 OpenStruct 的问题 [英] Problem using OpenStruct with ERB

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

问题描述

忘记包括我的环境信息... Win7x64,RubyInstaller Ruby v1.9.1-p378

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

EDIT 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 补丁 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.

我是使用 ERB 的新手,我能找到的示例是......嗯......不太有用......使用 ERB 玩了大约一个小时,我得到了一些基本的例子(终于),但我不知道为什么这不起作用......

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)

此代码产生以下错误:

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

why is it looking at the main:Object binding? I told it to use the binding from the OpenStruct by passing in vars_binding

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

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

推荐答案

解决问题:

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

Fix to Problem:

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

我是 Ruby 的新手,所以我无法解释发生了什么.我继续在网上搜索,发现这篇博文去工作.在修改您的示例以合并这种方法后,我最终得到以下工作代码:

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)

<小时>

附加信息:

当代码通过 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
_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

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

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