John Johnson wrote:

> On Tue, 2003-03-25 at 21:05, Jeremy wrote:
>> Hello again!
>> 
>> I've googled this one, as well as looking in Pickaxe and Ruby Way, and I
>> can't seem to find out anything about this.  What I want to do is create
>> a class that, when instanced, depending on the argument given to .new,
>> hides
>> some methods or others.  For example:
>> ahider = Hider.new(true)
>> 
>> This spits back out to me:
>> 
>> 3:in `initialize': undefined method `private_class_method' for
>> #<Hider:0x401c157c> (NameError)
>>         from test.rb:16:in `new'
>>         from test.rb:16
>> 
>> What I want to happen is when I issue "Hider.new(true)", I want it to
>> make
>> it so that method1 is private.  Likewise, when I do "Hider.new(false)", I
>> want it to make method2 private.
> 
> Would a Factory pattern work better in this case? I.e. have a base
> class, then derive the two different classes from that. Use a Factory to
> give you the instance you ask for. If you call
> Factory.instance(SOME_NAME) would return a SomeName instance, whereas
> Factory.instance(SOME_OTHER_NAME) would return a SomeOtherName instance.
> SomeName and SomeOtherName would both be subclasses of BaseClass.

It would indeed work, but I was hoping to be able to keep it down to one
object.  Here was something else I had tried:

class Hider

    private_class_method :new

    def initialize(val1,val2)
        @vartype1 = val1
        @vartype2 = val2
    end
    
    def Hider.type1
        new(true,false)
    end

    def Hider.type2
        new(false,true)
    end

    def Hider.type3
        new(true,true)
    end

    def method1
        puts "method 1" if @vartype1
    end

    def method2
        puts "method 2" if @vartype2
    end

end

Then how methods work depends on whether Hider.type1 or Hider.type2 is used.

a = Hider.type1
a.method1
a.method2

gives me:

>> method 1


While this works as expected, it doesn't really hide method2.  It just
doesn't execute the specified code if @vartype2 if false.  Not quite what I
was hoping for, but it gives a similar effect.

Thanks for your suggestion, though.  That was a mighty quick response!

Jeremy