From: kturing@... (kate turing) Date: 2002-01-27T18:13:59+09:00 Subject: Re: "friend" alternative in Ruby? Stared at the ceiling for a while and came up with this approach. Make Foo.initialize "return" a block that calls Foo.privateThing. You can only get such a block (precluding __send__, etc.) by being the one that called Foo.new. This is a bit more of a mess than using built-in-to-the-language access control, of course. But it's sort of cute. k. class FooMgr def initialize @privateBlocks = Hash.new end def createFoo r Foo.new r do |x| @privateBlocks[r] = x end end def frobulate r @privateBlocks[r].call end end class Foo def initialize r @r = r yield Proc.new {privateThing} end private def privateThing puts "private #{@r}" end end resources = ['r1', 'r2', 'r3'] fm = FooMgr.new #create a bunch of Foo objects foos = resources.collect { |r| fm.createFoo r } #frobulate them, indirectly calling Foo.privateThing resources.each { |r| fm.frobulate r } # This will throw a NameError, as desired foos[0].privateThing