I am creating a class to wrap around a Savon SOAP connection, as follows:
class SOAPConnection
attr_reader :url, :namespace
def initialize(url, namespace)
@url = url
@namespace = namespace
@client = Savon::Client.new do
wsdl.endpoint = @url
wsdl.namespace = @namespace
end
end
end
This code does not work. The wsdl document that gets initialized has a nil endpoint and a nil namespace.
To make the code work, I have to use the following:
class SOAPConnection
attr_reader :url, :namespace
def initialize(url, namespace)
@url = url
@namespace = namespace
@client = Savon::Client.new do
wsdl.endpoint = url # <=== use local variable
wsdl.namespace = namespace # <=== use local variable
end
end
end
Note that when setting up the wsdl.endpoint and wsdl.namespace I am using the local url and namespace variables, not the @url and @namespace instance variables.
So it seems that when passing in the block to initialize the wsdl document, the context of local variables is preserved while the context of instance variables is not. Is this a fundamental behaviour of Ruby?