# Cloning only when necessary

**URL:** <https://www.ruby-forum.com/t/cloning-only-when-necessary/178119>\
**Category:** Ruby\
**Created:** [October 29, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/cloning-only-when-necessary/178119 "2009-10-29T13:03:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 29, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/cloning-only-when-necessary/178119/1 "2009-10-29T13:03:12Z")

</div>

Hi,

if you want to make sure you get a copy of an instance that nobody  
else has access to you can #clone it. That’s not necessary if the  
instance is immutable or frozen (or both). Does anybody think it is a  
good idea to implement this? E.g.

class Object  
def frown  
frozen? ? self : clone  
end  
end

class Numeric  
def frown  
self  
end  
end

…

The you can do in constructor code for example

def initialize(something)  
@x = something.frown  
end

and be sure that you do not face aliasing issues with @x. Does  
anybody think we should have this in the language?

Note: The name is remotely inspired by “FReeze” and “OWN”. I’m not  
religious about it, not at all. It’s just that I could not make up a  
better one. Suggestions welcome.

Kind regards

robert
