aris
September 15, 2012, 3:48am
1
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 method
filename’ 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:in
result’
–
Kind Regards,
Rajinder Y.
SafetyNet Test Driven Development
http://safetynet.devmentor.org
Dev_G
September 15, 2012, 4:37am
2
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:in
result’
OK I tried the following in IRB and I get the same type of error?
root@karma:/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:in
result’
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
Dev_G
September 15, 2012, 4:56am
3
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 method
filename’ 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
September 16, 2012, 1:43pm
4
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.