# Concerning the #to\_\* convention

**URL:** <https://www.ruby-forum.com/t/concerning-the-to-convention/100321>\
**Category:** Ruby\
**Created:** [December 13, 2005, 6:18pm UTC](https://www.ruby-forum.com/t/concerning-the-to-convention/100321 "2005-12-13T18:18:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 13, 2005, 6:18pm UTC](https://www.ruby-forum.com/t/concerning-the-to-convention/100321/1 "2005-12-13T18:18:15Z")

</div>

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.new(:uri =\> opts[:namespace].to\_str,  
opts[:prefix])  
end  
end  
end  
end

I hope you can give me a hint of what the best practice is.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 13, 2005, 6:39pm UTC](https://www.ruby-forum.com/t/concerning-the-to-convention/100321/2 "2005-12-13T18:39:21Z")

</div>

> > > > > “D” == Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> I hope you can give me a hint of what the best practice is.

I don’t know what is the best practice but you can ask ruby

moulon% ruby -e ‘a = [1, 2]; b = a.to\_a; p a.object\_id==b.object\_id’  
true  
moulon%

moulon% ruby -e ‘a = “12”; b = a.to\_s; p a.object\_id==b.object\_id’  
true  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 13, 2005, 7:30pm UTC](https://www.ruby-forum.com/t/concerning-the-to-convention/100321/3 "2005-12-13T19:30:34Z")

</div>

ts wrote:

> moulon% ruby -e ‘a = “12”; b = a.to\_s; p a.object\_id==b.object\_id’  
> true  
> moulon%
> 
> Guy Decoux

Sweet! Thanks!

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 14, 2005, 9:28pm UTC](https://www.ruby-forum.com/t/concerning-the-to-convention/100321/4 "2005-12-14T21:28:48Z")

</div>

Daniel S. wrote:

> > moulon% ruby -e ‘a = “12”; b = a.to\_s; p a.object\_id==b.object\_id’  
> > Daniel

Those two examples are returning the same object because it is already  
in the desired form. If it is actually a change, you will get a new  
object:

irb(main):001:0\> a = 12  
=\> 12  
irb(main):002:0\> b = a.to\_s  
=\> “12”  
irb(main):003:0\> a.object\_id  
=\> 25  
irb(main):004:0\> b.object\_id  
=\> -605442624

-Justin
