0

Rob Conery の Tekpub/Rails 3 チュートリアル ビデオをフォローしています。ビデオ リリースの Rack のバージョン (1.1) と私のマシンの Rack のバージョン (1.4.5) の間で何かが変わったと思います。別の方法で何をする必要があるかを理解する方法がわかりません。

次のコードでは、文字列変数があり、それに文字列変数 (メソッドoutによって返される配列の 3 番目の要素) を連結しようとしています。MyApp.Call

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

しかし、私はエラーが発生します:

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

ここで何が欠けていますか?

4

1 に答える 1

2

配列に文字列を追加しようとしています。の 3 番目の要素

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

配列です。

join を使用して、その配列を文字列に変更できます。

response = @app.call(env)[2].join
于 2013-04-22T03:04:04.510 に答える