In the code below:
::Trace.tracer = ::Trace::ZipkinTracer.new()
what is the relation between Trace
and ZipkinTracer
This code does the following thing. First, it instantiates class ZipkinTracer
:
new_instance = Trace::ZipkinTracer.new()
Then, it calls #tracer=
method of the Trace
module:
Trace.tracer=( new_instance )
Ruby syntax allows this to be rewritten as
Trace.tracer = new_instance
In this case, no assignment is happening, but a method ending in =
is called. Methods ending in =
are allowed in Ruby, used generally for attribute assignment, and they are special in that they always return the assigned value (that is, their argument), regardless of what other return value you might be trying to prescribe:
class Foo
def bar=( value )
puts "Method #bar= called!"
@bar = value
puts "Trying to return Quux!"
return "Quux!"
end
def bar; @bar end
end
foo = Foo.new
foo.bar #=> nil
foo.bar = "Baz!"
#=> Method #bar= called!
#=> Trying to return Quux!
#=> "Baz!" -- attempt to explicitly return "Quux!" failed
foo.bar #=> "Baz!"