On Wed, 13 Jan 2010 11:11:34 +0900
Jeff <cohen.jeff / gmail.com> wrote:

> On Jan 12, 9:48am, Gregory Brown <gregory.t.br... / gmail.com> wrote:
> > On Tue, Jan 12, 2010 at 10:33 AM, Jeff <cohen.j... / gmail.com> wrote:
> > > But I haven't seen anyone else need to do this in their modules/
> > > classes, so now I'm wondering - has anyone else needed to do
> > > this? It seems like this would be a common problem for any
> > > shared Ruby library, so now I'm wondering why I *haven't* run
> > > into this before.
> >
> > As things get sufficiently, complex, I think that something like
> > this might be reasonable.  Prawn is edging on needing some support
> > for avoiding name clashes, since it carries a lot of internal state
> > in the Document class and utilizes many mixins.
> > I'm not sure if this is the convention we'll use, but it's one
> > possibility.
> 
> Gregory, that's interesting.  Maybe any Ruby library that reaches
> sufficent mass use will run into this problem.
> 
> But I think the underscore doesn't really solve the problem, right?
> 
> Let's say you use @_file in your module that I include.  You probably
> use #nodoc# around it so I don't even see it in the rdocs.  So then my
> class I'm about to do @file, but I think, someone might derive from my
> class, so I'd better call it @_file.  Collision again.
> 
> So I say, let's not use the hard-to-read underscores, and document
> ivars in the rdocs like everything else.
> 
> Of course, it's not that I like my solution either... :-(  I'm just
> not seeing how adding underscores solves the problem.
> 
> Jeff

I don't think it's about avoiding name clashes but a kind of 'Hungarian
Notation': When looking at a variable name you immediately see it's
intention. No need to browse any rdoc or check when it was first
initialized. I sometimes name variables with an underscore if they are
not intended to be accessed from the outside via attr, attr_reader etc..

martin