# Equivalent of const modifier

**URL:** <https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237>\
**Category:** Ruby\
**Created:** [February 12, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237 "2006-02-12T22:18:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237/1 "2006-02-12T22:18:30Z")

</div>

Very often, I’d like to ensure (and document) that a method will not  
change any of its parameters:

# This looks at, but doesn’t modify, a and b

def mymethod(a, b)  
end

In C++ I can do this with the const modifier.

Is there anyway to do this in Ruby?

I’d call freeze, except that would freeze the original a as well.  
I’d call dup/clone and then freeze, but those aren’t deep copies, from  
what I understand.

So, I guess my question boils down to:  
Is there a way to make a frozen, full (ie deep) copy of an object?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237/2 "2006-02-12T22:39:59Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’d call freeze, except that would freeze the original a as well.  
> I’d call dup/clone and then freeze, but those aren’t deep copies, from  
> what I understand.
> 
> So, I guess my question boils down to:  
> Is there a way to make a frozen, full (ie deep) copy of an object?

You can make a deep copy and then freeze the copy.

> **[WebMD - Better information. Better health.](https://www.webmd.com/?Make_A_Deep_Copy_Of_An_Object)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237/3 "2006-02-12T22:49:14Z")

</div>

DÅ?a NedeÄ¾a 12 FebruÃ¡r 2006 22:18 removed\_email\_address@domain.invalid napÃ­sal:

> Very often, I’d like to ensure (and document) that a method will not  
> change any of its parameters:

The most Ruby way would be plain not changing the parameters - you’re  
perfectly sure the method doesn’t do that when it doesn’t do that.  
Quoting  
James B. from a recent post: “Ruby assumes the developer is a  
grown-up.”

Also, I believe the convention would be that a method doesn’t clobber  
its  
parameters unless -that- is explicitly documented, so I wouldn’t go out  
of my  
way to document that a method does -not- change its parameters.

> # This looks at, but doesn’t modify, a and b
> 
> def mymethod(a, b)  
> end
> 
> In C++ I can do this with the const modifier.

Well, with the risk of stating the obvious, Ruby isn’t C++. Some things  
are  
plain done differently. Or not at all.

> Is there anyway to do this in Ruby?
> 
> I’d call freeze, except that would freeze the original a as well.  
> I’d call dup/clone and then freeze, but those aren’t deep copies, from  
> what I understand.
> 
> So, I guess my question boils down to:  
> Is there a way to make a frozen, full (ie deep) copy of an object?

Implement #initialize\_copy for deep-copy semantics, override #freeze to  
freeze  
instance attributes of the object before.

I wouldn’t bother though, as far as I know, freezing objects is only  
used when  
debugging, e.g. when you suspect a bug is due to an object being changed  
someplace you don’t expect it. It’s not encouraged for use in common  
code  
unless necessary.

David V.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 14, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237/4 "2006-02-14T22:14:41Z")

</div>

DÅ?a Pondelok 13 FebruÃ¡r 2006 21:38 Adam P. Jenkins napÃ­sal:

> > I’d call freeze, except that would freeze the original a as well.
> 
> Unless your code is multithreaded, you could always just unfreeze the  
> arguments when the function exits, since nothing else will be accessing  
> the objects while mymethod is executing.

I thought freezing objects was permanent?

David V.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 13, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/equivalent-of-const-modifier/66237/5 "2006-02-13T21:38:41Z")

</div>

> I’d call freeze, except that would freeze the original a as well.

Unless your code is multithreaded, you could always just unfreeze the  
arguments when the function exits, since nothing else will be accessing  
the objects while mymethod is executing.

David V. wrote:

> > # This looks at, but doesn’t modify, a and b
> > 
> > def mymethod(a, b)  
> > end
> > 
> > In C++ I can do this with the const modifier.
> 
> Well, with the risk of stating the obvious, Ruby isn’t C++. Some things are  
> plain done differently. Or not at all.

Also, C++'s const is like Ruby’s private, in that there’s an easy way to  
get around it: instance\_eval to get around private, const\_cast to get  
around const. So it’s just an advisory flag anyway, and doesn’t  
guarantee anything.
