D
Daniel Schierbeck
Howdy partners! I'm having a bit of fun writing a Ruby XML document
object model (yes, I'm aware of the fact that there already exists such
models), and I'm using #to_* throughout the code. I'm just wondering if
a #to_* method always should return a copy of the receiver, in the
format specified by * (e.g. `str', `i'), or if it's okay to just pass a
reference to the receiver itself?
Consider this:
class Namespace
def to_xml_ns
self # should this be a copy?
end
end
class Element < Node
def initialize(opts = {})
if opts[:namespace]
# `namespace' quacks like a Namespace object
if opts[:namespace].respond_to? :to_xml_ns
# I'd like a Namespace object in @namespace.
@namespace = opts[:namespace].to_xml_ns
# `namespace' quakcs like a String object
elsif opts[:namespace].respond_to? :to_str
@namespace = Namespace.newuri => opts[:namespace].to_str,
opts[refix])
end
end
end
end
I hope you can give me a hint of what the best practice is.
Cheers,
Daniel
object model (yes, I'm aware of the fact that there already exists such
models), and I'm using #to_* throughout the code. I'm just wondering if
a #to_* method always should return a copy of the receiver, in the
format specified by * (e.g. `str', `i'), or if it's okay to just pass a
reference to the receiver itself?
Consider this:
class Namespace
def to_xml_ns
self # should this be a copy?
end
end
class Element < Node
def initialize(opts = {})
if opts[:namespace]
# `namespace' quacks like a Namespace object
if opts[:namespace].respond_to? :to_xml_ns
# I'd like a Namespace object in @namespace.
@namespace = opts[:namespace].to_xml_ns
# `namespace' quakcs like a String object
elsif opts[:namespace].respond_to? :to_str
@namespace = Namespace.newuri => opts[:namespace].to_str,
opts[refix])
end
end
end
end
I hope you can give me a hint of what the best practice is.
Cheers,
Daniel