title: Ruby: erb throwing error "`result': can't convert String into Integer (TypeError)"
body: <p>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'.</p>
  
  <p>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.</p>
  
  <p>Here's the class that does the work (generate_interface.rb):</p>
  
  <pre><code>#! /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}" )
      @outfile=@erbfiles.map 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, "%&lt;&gt;" )
        puts( "message: #{message.inspect}" )
        result=message.result
        puts( "result: #{result.inspect}" )
        File.open(outfile, 'w' ) { |f| f.write( message.result) }
      end
    end
  end
  </code></pre>
  
  <p>The yaml file (test.yaml):</p>
  
  <pre><code>--- 
  test_id: XXX123
  </code></pre>
  
  <p>The template file (test.txt.erb):</p>
  
  <pre><code>Line 1
  Line 2 test_id: &lt;%= i['test_id'] %&gt;
  Line 3
  </code></pre>
  
  <p>The code that drives the whole mess (test.rb):</p>
  
  <pre><code>#! /usr/bin/ruby
  
  require "generate_interface"
  
  test_interface = GenerateInterface.new( "test.yaml" )
  test_interface.gobutton
  </code></pre>
  
  <p>And finally, the output and error messages:</p>
  
  <pre><code>$ 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"=&gt;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: &lt;%= i['test_id'] %&gt;\nLine 3\n"
  message: #&lt;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="%&lt;&gt;", @filename=nil&gt;
  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
  </code></pre>
  
  <p>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.</p>