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

Discussion in 'Ruby' started by Sam Roberts, Feb 26, 2005.

  1. Sam Roberts

    Sam Roberts Guest

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

    class Foo
    attr_writer :eek:pt
    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 26, 2005
    #1
    1. Advertising

  2. * Sam Roberts (Feb 27, 2005 00:50):
    > class Foo
    > attr_writer :eek:pt
    > 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 :eek:pt
    def initialize
    yield self
    undef :eek:pt=
    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);}
     
    Nikolai Weibull, Feb 27, 2005
    #2
    1. Advertising

  3. Hi --

    On Sun, 27 Feb 2005, Sam Roberts wrote:

    > I want to allow method calls during initialization, but not after:
    >
    > class Foo
    > attr_writer :eek:pt
    > 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
     
    David A. Black, Feb 27, 2005
    #3
  4. Sam Roberts

    Sam Roberts Guest

    Thank you both!

    Sam

    Quoting , on Sun, Feb 27, 2005 at 09:00:33AM +0900:
    > class Foo
    > attr_writer :eek:pt
    > def initialize
    > yield self
    > undef :eek:pt=
    > end
    > end
    >
    > nikolai


    Quoting , on Sun, Feb 27, 2005 at 06:45:24PM +0900:
    > Try:
    >
    > class << self
    > undef_method 'opt='
    > end
    >
    > David
     
    Sam Roberts, Mar 8, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    1
    Views:
    608
    S. Justin Gengo
    Dec 12, 2005
  2. Kenneth McDonald
    Replies:
    5
    Views:
    322
    Kenneth McDonald
    Sep 26, 2008
  3. thunk
    Replies:
    1
    Views:
    315
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    487
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    625
    thunk
    Apr 3, 2010
Loading...

Share This Page