Ruby on Rails / Rack Builder“使用”方法错误的参数数量

时间:2022-08-03 23:20:10

I am new to Ruby and Rails. I am playing with Rack, trying to get a basic understanding of this peice of the Rails puzzle, following along with Rob Conery in his Tekpub/Rails 3 tutorial vid.

我是Ruby和Rails的新手。我正在玩Rack,尝试基本了解Rails拼图的这个问题,然后在他的Tekpub / Rails 3教程视频中跟随Rob Conery。

Unfortunately, the version of Rack used in the vid has become long in the tooth, and I think something has changed in between the video release and now (as have some things in Ruby between 1.8.x and 1.9.x). Even more unfortunately, I don't yet know enough about Ruby or Rack to know how to figure out what I need to do differently. The version of Rack used in the video is 1.1. The version on my machine is 1.4.5.

不幸的是,在vid中使用的Rack的版本已经变得很长,我认为在视频版本和现在之间发生了一些变化(在Ruby中介于1.8.x和1.9.x之间)。更不幸的是,我还不太了解Ruby或Rack,知道如何弄清楚我需要做些什么。视频中使用的Rack版本为1.1。我机器上的版本是1.4.5。

Silly example code:

愚蠢的示例代码:

class EnvironmentOutput

  def intialize(app)
    @app = app
  end

  def call(env)
    out = ""

    unless(@app.nil?)
      response = @app.call(env)[1]
      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

# My understanding is that this should work:
use EnvironmentOutput
run MyApp.new

When I run this, I get the following:

当我运行它时,我得到以下内容:

ArgumentError: wrong number of arguments(1 for 0)

This is where the first in a series of errors occurs (line 82 in the rack Builder class):

这是一系列错误中的第一个发生的位置(机架Builder类中的第82行):

def use(middleware, *args, &block)
  if @map
    mapping, @map = @map, nil
    @use << proc { |app| generate_map app, mapping }
  end
  # error occurs HERE:
  @use << proc { |app| middleware.new(app, *args, &block) }
end

Obviously, I am passing something incorrectly. Sadly, I don't yet know enough to figure out what it is I am doing wrong. I have searched on Google and here on SO, but I suspect I also don't have quite a strong enough grasp on the Ruby/Rails/Rack relationship to know what to ask and get a reasonably helpful result (or, if I AM, then I don't yet recognize it).

显然,我错误地传递了一些东西。可悲的是,我还不知道究竟是什么我做错了。我在Google上搜索过,但是我怀疑我对Ruby / Rails / Rack关系的掌握程度还不够,知道要问什么并得到一个合理有用的结果(或者,如果我,然后我还没认识到它)。

Does anyone know what I am doing wrong here?

有谁知道我在做错了什么?

UPDATE: Thanks to the selected answer, I realize it was a typo. Next issue is an array-to-string conversion problem in the same code, but will post as new question.

更新:由于选择的答案,我意识到这是一个错字。下一个问题是同一代码中的数组到字符串转换问题,但将作为新问题发布。

1 个解决方案

#1


3  

It’s just a typo:

这只是一个错字:

def intialize(app)

should be

应该

def initialize(app)

(you’ve missed an i).

(你错过了我)。

Since you don’t provide an initialize method, Ruby tries to use the default, argument-less one, but since Rack passes an argument (the app) you get the ArgumentError.

由于您没有提供初始化方法,因此Ruby尝试使用默认的无参数方法,但由于Rack传递了一个参数(应用程序),因此您将获得ArgumentError。

#1


3  

It’s just a typo:

这只是一个错字:

def intialize(app)

should be

应该

def initialize(app)

(you’ve missed an i).

(你错过了我)。

Since you don’t provide an initialize method, Ruby tries to use the default, argument-less one, but since Rack passes an argument (the app) you get the ArgumentError.

由于您没有提供初始化方法,因此Ruby尝试使用默认的无参数方法,但由于Rack传递了一个参数(应用程序),因此您将获得ArgumentError。