Problem using OpenStruct with ERB

前端 未结 4 1812
旧时难觅i
旧时难觅i 2020-12-16 14:32

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

EDIT 2: just updated to v1.9.1, patch 429, a

4条回答
  •  天命终不由人
    2020-12-16 15:18

    What's your environment look like? This code worked for me (I just changed the string "bar" to "baz" to disambiguate in my brain, and added it to the template):

    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)
    

    When I run it, I get:

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

    Under 1.8.7 on OSX:

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

提交回复
热议问题