XIVSolutions
XIVSolutions

Reputation: 4502

Ruby on Rails/Rack 1.4.5 return from Call Method / Can't Convert String to Array

I am following Rob Conery in his Tekpub/Rails 3 tutorial video. I think something has changed in between the versions of Rack in video release (1.1) and the one on my machine (1.4.5). I don't know how to figure out what I need to do differently.

In the following code, I have a string variable out, and am attempting to concatenate a string variable (the third element of the array returned by MyApp.Call method) onto it.

class EnvironmentOutput

  def initialize(app)
    @app = app
  end

  def call(env)
    out = ""

    unless(@app.nil?)
      response = @app.call(env)[2]

      # The Problem is HERE (Can't Convert Array to String):
      out+=response
    end

    env.keys.each {|key| out+="<li>#{key}=#{env[key]}"}
    ["200", {"Content-Type" => "text/html"}, [out]]
  end

end

class MyApp
  def call(env)
    ["200", {"Content-Type" => "text/html"}, ["<h1>Hello There</h1>"]]    
  end
end

use EnvironmentOutput
run MyApp.new

But I get the error:

"Can't Convert Array to String" at `out+=response` 

What am I missing here?

Upvotes: 0

Views: 227

Answers (1)

fengolly
fengolly

Reputation: 503

You're trying to add a string to an array. The third element of

["200", {"Content-Type" => "text/html"}, ["<h1>Hello There</h1>"]] is ["<h1>Hello There</h1>"]

is an array.

You can change that array into a string with join:

response = @app.call(env)[2].join

Upvotes: 2

Related Questions