2013-11-26 41 views
0

我試圖在HelloWorld機架程序下面執行並出現錯誤。感謝任何幫助。HelloWorld Rack Program:NoMethodError:undefined method'each'

紅寶石版本紅寶石1.9.3p448(2013年6月27日)[x86_64的-的cygwin]

# helloworld.rb 
require 'rack' 
require 'rack/server' 

class HelloWorld 
def response 
    [200, {}, 'Hello World'] 
end 
end 

class HelloWorldApp 
def self.call(env) 
    HelloWorld.new.response 
end 
end 

Rack::Server.start :app => HelloWorldApp 

ERROR

[2013-11-26 11:23:03] ERROR NoMethodError: undefined method `each' for "Hello World":String 
    /usr/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/handler/webrick.rb:72:in `service' 
    /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 

回答

11

變化:

def response 
    [200, {}, 'Hello World'] 
end 

到:

def response 
    [200, {}, ['Hello World']] 
end 
3

#response返回的數組中的第3項(即, body)必須對#each方法做出響應。數組例如,respond to #each

Rack specification

The Body

The Body must respond to each and must only yield String values. The Body itself should not be an instance of String, as this will break in Ruby 1.9. If the Body responds to close, it will be called after iteration. If the body is replaced by a middleware after action, the original body must be closed first, if it responds to close. If the Body responds to to_path, it must return a String identifying the location of a file whose contents are identical to that produced by calling each; this may be used by the server as an alternative, possibly more efficient way to transport the response. The Body commonly is an Array of Strings, the application instance itself, or a File-like object.

+0

感謝您的參考。 – Venu