From: Robert Klemme Date: 2007-11-10T02:41:35+09:00 Subject: Re: Information Hiding 2007/11/9, arcadiorubiogarcia@gmail.com : > Hi, > > I'm quite new to Ruby, so please excuse me if this question is a bit > silly. > > Consider: > > class Foo > def initialize > @a = [] > end > > def bar(p) > @a << p > end > end > > It's nice that Ruby returns the last evaluated expression, without > putting an explicit return. However, Foo#bar dangerously exposes the > Foo class internals. > > My question is, what is the common idiom to avoid this? Simply adding > a return nil? def bar(p); @a << p; return nil; end In this case I'd do def bar(x) @a << x self end Because then you can nicely chain calls to #bar. Depending on circumstances you might even consider implementing #<< instead of / additionally to #bar. Kind regards robert -- use.inject do |as, often| as.you_can - without end