DÅ?a NedeÄ¾a 12 FebruÃ¡r 2006 22:18 email@example.com 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.
James B. from a recent post: “Ruby assumes the developer is a
Also, I believe the convention would be that a method doesn’t clobber
parameters unless -that- is explicitly documented, so I wouldn’t go out
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)
In C++ I can do this with the const modifier.
Well, with the risk of stating the obvious, Ruby isn’t C++. Some things
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
instance attributes of the object before.
I wouldn’t bother though, as far as I know, freezing objects is only
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