From: Logan Capaldo Date: 2005-05-30T06:03:01+09:00 Subject: Re: method visibility On 5/28/05, Bob Aman wrote: > More nubyness. Apologies. > > Ok, so, I have this rss feed caching system i'm working on. Ideally, > I'd like to have as little of the internal workings of the thing > visible as possible. I just want the programmer to interact with Feed > and FeedItem objects, not the caching mechanisms. > > It seems to me that I don't really want the programmer to be able to > create instances of a FeedItem manually. All FeedItems ought to be > created automatically by a Feed object. But it seems like ruby lacks > the direct equivalent of c++'s "friend", so I guess I can't give > access to the "new" class method only to Feed. > > Also, I'd like to make the caching mechanism be changable. It'd be > nice to be able to switch between, say, a database-based cache and a > file system-based cache. Perhaps like this: > > FeedCache.cache_type = :DatabaseFeedCache > > And then the Feed object would simply use the DatabaseFeedCache behind > the scenes. But again, I'm not sure how to make the cache's behavior > available only to the Feed and FeedItem classes (or even if I can). > -- > Bob Aman > > You could do one of these: class A private_class_method :new end class FriendOfA def make_an_A A.class_eval { new } end end Of course still doesn't stop anyone from just doing a class_eval themselves, but it does make it slightly more discouraging, and they definitely won't do it by accident.