Equivalent of const modifier

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?

[email protected] 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.

DÅ?a Nedeľa 12 Február 2006 22:18 [email protected] 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.

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.

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.