Using erb to generate a file

Hey guys I am generating a file using erb and an input template file.

def fileSafeCreateHeader( filename )

puts "RMake> Creating header file: " + filename

rmake_loc = File.expand_path( File.dirname( FILE ) )
lines = IO.readlines( “#{rmake_loc}/templates/title.trb” )

File.open( filename, “w+” ) do |f|
expanded_line = ERB.new( lines.join("\n") , nil, “%<>” )
f.puts expanded_line.result
end

end

However when I run my script I get an error saying ‘filename’ is not
defined, when you can see it is! What’s going on here?

RMake> Creating header file: def.h
(erb):3:in <main>': undefined local variable or methodfilename’ for
main:Object (NameError)
from /usr/local/lib/ruby/1.9.1/erb.rb:838:in eval' from /usr/local/lib/ruby/1.9.1/erb.rb:838:inresult’


Kind Regards,
Rajinder Y.

SafetyNet Test Driven Development
http://safetynet.devmentor.org

On Fri, Sep 14, 2012 at 9:46 PM, Dev G. [email protected] wrote:

expanded_line = ERB.new( lines.join("\n") , nil, "%<>" )

main:Object (NameError)
from /usr/local/lib/ruby/1.9.1/erb.rb:838:in eval' from /usr/local/lib/ruby/1.9.1/erb.rb:838:inresult’

OK I tried the following in IRB and I get the same type of error?

[email protected]:/opt/ruby-1.9.3-p194# irb
irb(main):001:0> require ‘erb’
=> true
irb(main):002:0>
irb(main):003:0* weekday = Time.now.strftime(’%A’)
=> “Friday”
irb(main):004:0> simple_template = “Today is <%= weekday %>.”
=> “Today is <%= weekday %>.”
irb(main):005:0>
irb(main):006:0* renderer = ERB.new(simple_template)
=> #<ERB:0x000000033558b0 @safe_level=nil,
@src="#coding:UTF-8\n_erbout = ‘’; _erbout.concat "Today is “;
_erbout.concat(( weekday ).to_s); _erbout.concat “.”;
_erbout.force_encoding(ENCODING)”, @enc=#Encoding:UTF-8,
@filename=nil>
irb(main):007:0> puts output = renderer.result()
NameError: undefined local variable or method weekday' for main:Object from (erb):1:in
from /usr/local/lib/ruby/1.9.1/erb.rb:838:in eval' from /usr/local/lib/ruby/1.9.1/erb.rb:838:inresult’
from (irb):7
from /usr/local/bin/irb:12:in `’
irb(main):008:0>

What’s am I missing??


Kind Regards,
Rajinder Y.

SafetyNet Test Driven Development
http://safetynet.devmentor.org

On Fri, Sep 14, 2012 at 10:36 PM, Dev G. [email protected] wrote:

File.open( filename, “w+” ) do |f|
(erb):3:in <main>': undefined local variable or methodfilename’ for
irb(main):002:0>
@filename=nil>

OK it seems, for things to work I need to pass in ‘binding’ and make
the call like:

puts output = renderer.result(binding)


Kind Regards,
Rajinder Y.

SafetyNet Test Driven Development
http://safetynet.devmentor.org

Dev G. wrote in post #1076095:

OK it seems, for things to work I need to pass in ‘binding’ and make
the call like:

puts output = renderer.result(binding)

That’s correct. Without a Binding object the result() method cannot
access the local variables in the caller’s scope.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs