When I need to return immutable collection from my class in C# or Java I return it as IEnumerable interface. What is the standard way for this in Ruby? Clone? Freeze?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want a
RuntimeErrorraised if modification is attempted, then usefreeze(). If you only want to prevent the caller from modifying your data, thenclone()is a good choice.Keep in mind that freeze() might not work like you expect.
Personally, I’ve never had need for
freeze(), butclone()is often useful.