Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3978956
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:06:39+00:00 2026-05-20T05:06:39+00:00

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

  • 0

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

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

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)

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:06:40+00:00Added an answer on May 20, 2026 at 5:06 am

    Fix to Problem:

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

    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)
    

    Additional Information:

    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
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.