0

我正在使用 Ruby 和 Savon 编写一个小客户端。界面从 0.7 版本显着更改为 0.8.x。我所有的调用都不再起作用了:-(。我怎样才能传递一个本地成员变量。请看这个例子,@userName 和@userPassword 没有在块中定义。


begin
    @response = @authentication_svc.request :wsdl, "AuthenticateUser" do  
        http.headers["SOAPAction"] = "AuthenticateUser"  
        soap.body = "#{@userName}#{@passwd}"  
    end  
rescue Savon::SOAP::Fault => e  
    @last_soap_error = e.message  
end  

4

3 回答 3

2

在更仔细地阅读了变更日志后,我了解到 Savon 使用 instance_eval 来执行该块。因此,不能在该块中使用另一个类的实例变量。变更日志中也说明了这一点。为了解决我的问题,我将实例变量的值分配给了一个局部变量。这对我有用。

于 2011-02-08T17:48:12.930 回答
1

恐怕你的问题没有多大意义。

  • 块可以访问块外可用的任何东西。
  • @userName并且@passwd是实例变量,而不是局部变量或类变量。Ruby 没有任何通常称为“成员变量”的东西。在任何情况下,如果它们是在您进行此调用的类中设置的,您可以在块内很好地访问它们。

作为脚注,约定是使用下划线而不是驼峰式大小写来命名 Ruby 中的变量——@user_name而不是@userName.

于 2011-02-06T08:01:16.247 回答
0

为了避免必须将所有内容分配给局部变量,请编写从块内部获取对象(如soap和http)的方法。因为方法属于类(而不是实例),它们仍然可以从块中调用,但是一旦你在方法的上下文中,你的实例变量对你是可用的。

def do_request
  begin
    @response = @authentication_svc.request :wsdl, "AuthenticateUser" do  
      prepare_soap(soap,http)
    end
  rescue Savon::SOAP::Fault => e  
    @last_soap_error = e.message  
  end 
end

def prepare_soap(soap, http)
  http.headers["SOAPAction"] = "AuthenticateUser"  
  soap.body = "#{@userName}#{@passwd}"  
end
于 2011-03-09T02:00:24.943 回答