Da Nedea 12 Februr 2006 22:18 eastcoastcoder / gmail.com napsal: > 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 Britt 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 Vallner