From: Kent Dahl Date: 2002-06-26T21:13:58+09:00 Subject: Re: []<< Tom Sawyer wrote: > tt['test'] << '..2' # how come you can do me? Keep in mind that the first part just returns an object. Once you have a reference to an object, you can call any method you like on it, that isn't private or protected. This includes <<. > it seems to me that the main point of accessors and the []= method is to > control accessiblity to a class' instance variables. right? but as this > example shows, there are ways in which "back-doors" exist. If you let your objects out to the surrounding world unprotected (or unfrozen) these things will happen. Take a look at the dup suggestion from Jim, or consider freezing your objects before returning them. (This will mean that you have to create a new object if you want to change it on the inside, and deny any access on the outside. Sounds like a matter of preference and optimization consideration.) Or you could be nasty and do like this before you return: $ irb irb(main):001:0> a = [1,2,3] [1, 2, 3] irb(main):002:0> class < a << 5 NameError: private method `<<' called for [1, 2, 3]:Array from (irb):3 > shouldn't there be better control of such thing? i came across this when > i was actually trying to create a []<< method, and realized that i have > never seen such a beast before! shouldn't such a thing exist? No. You would then need [](insert any mutator method of the returned object here) methods, which would drive you bonkers in the end, since you can't possibly be sure about them all. Freezing or dup-ing sounds alot more sensible. -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 4. year ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)