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?

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 Yadav

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

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 `<main>'
        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 `<main>'
irb(main):008:0>

What's am I missing??

···

On Fri, Sep 14, 2012 at 9:46 PM, Dev Guy <devguy.ca@gmail.com> wrote:

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?

> 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 Yadav

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

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

puts output = renderer.result(binding)

···

On Fri, Sep 14, 2012 at 10:36 PM, Dev Guy <devguy.ca@gmail.com> wrote:

On Fri, Sep 14, 2012 at 9:46 PM, Dev Guy <devguy.ca@gmail.com> wrote:

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?

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

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 `<main>'
        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 `<main>'
irb(main):008:0>

What's am I missing??

--
Kind Regards,
Rajinder Yadav

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

Dev Guy 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.

···

--
Posted via http://www.ruby-forum.com/\.