On Tue, 2003-03-25 at 21:45, Jeremy wrote:
> 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
...
> >> 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.

You must also consider the maintainability of the code. I'm sure you
have a good reason for wanting it to be one class, but does that
outweigh being able to understand how it works when you come back to it
next year? Or next month, in my case :-)

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

You're welcome!

Regards,
  JJ