Forum: Ruby Equivalent of const modifier

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-02-12 23:18
(Received via mailing list)
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?
Timothy H. (Guest)
on 2006-02-12 23:39
(Received via mailing list)
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.
http://www.rubygarden.org/ruby?Make_A_Deep_Copy_Of_An_Object
David V. (Guest)
on 2006-02-12 23:49
(Received via mailing list)
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.
Adam P. Jenkins (Guest)
on 2006-02-13 22:38
(Received via mailing list)
> 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.
David V. (Guest)
on 2006-02-14 23:14
(Received via mailing list)
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.
This topic is locked and can not be replied to.