谁在 Savon 的 SOAP 调用中使用类变量?
我正在使用 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
I'm writing a little client using Ruby and Savon. The interface changed significantly from version 0.7 to 0.8.x. All my calls don't work anymore :-(.
How can I pass on a local member variable.
Please see the example, @userName and @userPassword are not defined within the block.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更仔细地阅读变更日志后,我了解到 Savon 使用 instance_eval 来执行该块。因此,不能在该块中使用另一类的实例变量。变更日志中也说明了这一点。
为了解决我的问题,我将实例变量的值分配给局部变量。这对我来说很有效。
After reading the changelog more carefully I understood that Savon uses instance_eval to execute the block. Therefore one cannot use instance variables of another class within that block. That's also stated in the changelog.
To solve my problem I assigned the value of the instance variable to a local variable. That did the trick for me.
恐怕你的问题没有多大意义。
@userName
和@passwd
是实例变量,而不是本地变量或类变量。 Ruby 没有任何通常称为“成员变量”的东西。无论如何,如果它们是在您进行此调用的类中设置的,则您可以在块内很好地访问它们。作为脚注,在 Ruby 中命名变量时,惯例是使用下划线而不是驼峰式大小写 -
@user_name
而不是@userName
。I'm afraid your question does not make much sense.
@userName
and@passwd
are instance variables, not local or class variables. Ruby doesn't have anything normally called "member variables." In any case, if they are set in the class where you are making this call, you can access them just fine within the block.As a footnote, the convention is to use underscores and not camel case for naming variables in Ruby --
@user_name
instead of@userName
.为了避免将所有内容分配给局部变量,请编写从块内部获取对象(例如soap和http)的方法。因为这些方法属于类(而不是实例),所以仍然可以从块内调用它们,但是一旦处于方法的上下文中,实例变量就可供您使用。
To avoid having to assign everything to local variables, write methods which take objects (like soap and http) from inside the block. Because the methods belong to the class (not the instance), they can still be called from within the block, but once you are in the context of the method, your instance variables are available to you.