Barton Chittenden Barton Chittenden - 2 years ago 85
Ruby Question

Ruby: erb throwing error "`result': can't convert String into Integer (TypeError)"

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")

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}" ) do
puts( "erbfile: #{erbfile.inspect}" )
outfile = erbfile.gsub(/\.erb$/,"")
puts( "outfile: #{outfile.inspect}" )
template = erbfile, 'r' ) { |f| }
puts( "template: #{template.inspect}" )
message =, "%<>" )
puts( "message: #{message.inspect}" )
puts( "result: #{result.inspect}" ), 'w' ) { |f| f.write( message.result) }

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 = "test.yaml" )

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.

Answer Source

(Your paste of generate_interface.rb missed the final end)

The immediate problem is that the second argument to Erb.initialize is the safe level; the third being the trim options. Your trim options are being interpreted as a safe level.

To fix that, change

  message =, "%<>" )


  message =, nil, "%<>" )

The second problem is that variable i is not automatically available to your template code. To fix that, you need to pass in a binding, changing

  result = message.result


  result = message.result(binding)

In the line following, change message.result to result

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download