From: Rich Kilmer Date: 2002-01-27T01:48:30+09:00 Subject: RE: "friend" alternative in Ruby? Kate, From my perspective, you have a method that you want to be called by some objects, and not others. This seems to me a traditional security issue (like, at the human level of security) where when we want some people to be able to do things and not others. We solve this by giving them a password/key to use. How about this: class FooMgr def initialize @privkey = rand #=> random floating point value @foos = {} end def createFoo(resource) f = Foo.new(self, @privkey, resource) @foos[f.somekey]=f #=> where somekey is the way to # retrieve a foo return f end def [](somekey) return @foos[somekey] end def frobulate(somekey) #=> Nice method name ;-) @foos[somekey].doPrivateThing(@privkey) end end end in class Foo class Foo def initialize(fooMgr, privkey, resource) raise "FooMgr creates Foo" unless fooMgr.kind_of? FooMgr @privkey = privkey @resource = resource end def somekey ... return some key to retreive this by end def doPublicThing ... end def doPrivateThing(key) raise "Invalid key" unless key==@privkey # this could only be called by the FooMgr that created # this object because only it knows the key end end -Rich > -----Original Message----- > From: kate turing [mailto:kturing@yahoo.com] > Sent: Saturday, January 26, 2002 5:13 AM > To: ruby-talk ML; undisclosed-recipients: > Subject: Re: "friend" alternative in Ruby? > > > Mike Sassak wrote in message > news:<1012006343.5969.3.camel@synecdoche>... > > Could you elaborate on how you want your program to work? > > Thanks for the response. I am doing something very similar > to your second example. > > I'll try to flesh out my problem a little more without > dragging the application's obscure domain into the problem. > FooMgr is a factory and manager for Foo objects. I'm doing > this sort of this: > > fm = FooMgr.new > resources.each do |r| > f = fm.createFoo(r) > end > > # Assume there's a FooMgr.[] method that returns a foo. > # I want this to work. > fm[somekey].doPublicThing > > # I want this to not work. > fm[somekey].doPrivateThing > > # and i want this method to work > def FooMgr.frobulate > #@foos is a private > @foos[somekey].doPrivateThing > end > > Also, I'd like to enforce that Foo objects can only > be created by a FooMgr. The first possibility that > comes to mind is protecting Foo.initialize in the > same way that I'd like to protect Foo.doPrivateThing. > > Looking into #send was an interesting read. But it > feels a little low-level. Well, a lot. > > I don't think I can cleanly do exactly what I want in > Ruby. Maybe I just need to get comfortable with leaving > doPrivateThing public, as much as it pains me. BUT, I > suspect that someone out there (who is less tainted by > C++) has a Ruby-friendly way of accomplishing the same > thing. Hope they speak up. > > k >