Read-Only Masses in Ruby

When I need to return an immutable collection from my class in C # or Java, I return it as an IEnumerable interface. What is the standard way to do this in Ruby? Clone? Freezing?

+4
source share
1 answer

If you want RuntimeError raise if an attempt was made to modify, use freeze() . If you want the caller to not be able to modify your data, then clone() is a good choice.

Keep in mind that freeze () may not work as you expect .

Personally, I never needed freeze() , but clone() often useful.

+4
source

All Articles