Can I remove methods from a single object, but not its class?

I want to allow method calls during initialization, but not after:

  class Foo
   attr_writer :opt
   def initialize
     yield self

     # doesn't work
     remove_method 'opt='

     # doesn't work
     class < self
       remove_method 'opt='
     end

     # there must be a way???
   end
  end

  f = Foo.new { |s| s.opt = 4 }

  # I want this to fail!
  f.opt = 1

This has to be possible... whats the magic? Should I use
#method_missing, so I can decide to respond to it only during
initialization?

Thanks,
Sam

* Sam Roberts (Feb 27, 2005 00:50):

  class Foo
   attr_writer :opt
   def initialize
     yield self

     # doesn't work
     remove_method 'opt='

     # doesn't work
     class < self
       remove_method 'opt='
     end

     # there must be a way???
   end
  end

  f = Foo.new { |s| s.opt = 4 }

  # I want this to fail!
  f.opt = 1

This has to be possible... whats the magic? Should I use
#method_missing, so I can decide to respond to it only during
initialization?

class Foo
  attr_writer :opt
  def initialize
    yield self
    undef :opt=
  end
end

  nikolai

···

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Hi --

···

On Sun, 27 Feb 2005, Sam Roberts wrote:

I want to allow method calls during initialization, but not after:

class Foo
  attr_writer :opt
  def initialize
    yield self

    # doesn't work
    remove_method 'opt='

    # doesn't work
    class < self
      remove_method 'opt='
    end

Try:

    class << self
      undef_method 'opt='
    end

David

--
David A. Black
dblack@wobblini.net

Thank you both!

Sam

Quoting mailing-lists.ruby-talk@rawuncut.elitemail.org, on Sun, Feb 27, 2005 at 09:00:33AM +0900:

class Foo
  attr_writer :opt
  def initialize
    yield self
    undef :opt=
  end
end

  nikolai

Quoting dblack@wobblini.net, on Sun, Feb 27, 2005 at 06:45:24PM +0900:

···

Try:

   class << self
     undef_method 'opt='
   end

David