Quick background: I'm setting up a quick and dirty templating scheme where all of my template files are named '*.erb'. the fill in data resides in a yaml file. Output is to files with the name of the template, minus '.erb'.
I haven't done much work with erb, and I'm getting the error "`result': can't convert String into Integer (TypeError)" ... which doesn't make much sense to me.
Here's the class that does the work (generate_interface.rb):
#! /usr/bin/ruby
require 'yaml'
require 'erb'
class GenerateInterface
def initialize(yamlfile)
@yamlfile = yamlfile
@erbfiles = Dir.glob("*.erb")
end
def gobutton
# i -- interface. kept short because it's used all over the place in the erb files.
i = YAML.load_file( @yamlfile )
puts( "i: #{i.inspect}" )
puts( "i['test_id']: #{i['test_id'].inspect}" )
puts( "( i['test_id'] ).to_s: #{( i['test_id'] ).to_s}" )
@[email protected] do
|erbfile|
puts( "erbfile: #{erbfile.inspect}" )
outfile = erbfile.gsub(/\.erb$/,"")
puts( "outfile: #{outfile.inspect}" )
template = File.open( erbfile, 'r' ) { |f| f.read }
puts( "template: #{template.inspect}" )
message = ERB.new(template, "%<>" )
puts( "message: #{message.inspect}" )
result=message.result
puts( "result: #{result.inspect}" )
File.open(outfile, 'w' ) { |f| f.write( message.result) }
end
end
The yaml file (test.yaml):
---
test_id: XXX123
The template file (test.txt.erb):
Line 1
Line 2 test_id: <%= i['test_id'] %>
Line 3
The code that drives the whole mess (test.rb):
#! /usr/bin/ruby
require "generate_interface"
test_interface = GenerateInterface.new( "test.yaml" )
test_interface.gobutton
And finally, the output and error messages:
$ ruby -d test.rb
Exception `NoMethodError' at /usr/lib/ruby/1.8/rational.rb:78 - undefined method `gcd' for Rational(1, 2):Rational
i: {"test_id"=>XXX123}
i['test_id']: XXX123
( i['test_id'] ).to_s: XXX123
erbfile: "test.txt.erb"
outfile: "test.txt"
template: "Line 1\nLine 2 test_id: <%= i['test_id'] %>\nLine 3\n"
message: #<ERB:0xb74ac150 @src="_erbout = ''; _erbout.concat \"Line 1\\nLine 2 test_id: \"\n; _erbout.concat(( i['test_id'] ).to_s); _erbout.concat \"\\nLine 3\\n\"\n\n; _erbout", @safe_level="%<>", @filename=nil>
Exception `TypeError' at /usr/lib/ruby/1.8/erb.rb:715 - can't convert String into Integer
/usr/lib/ruby/1.8/erb.rb:715:in `result': can't convert String into Integer (TypeError)
from /usr/lib/ruby/1.8/erb.rb:714:in `call'
from /usr/lib/ruby/1.8/erb.rb:714:in `result'
from ./generate_interface.rb:26:in `gobutton'
from ./generate_interface.rb:17:in `map'
from ./generate_interface.rb:17:in `gobutton'
from test.rb:6
I figured that there was something wrong with _erbout.concat(( i['test_id'] ).to_s), but when I explicitly print ( i['test_id'] ).to_s I get 'XXX123' which is what I expect.