[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Sam Roberts

2/26/2005 11:45:00 PM

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



3 Answers

Nikolai Weibull

2/27/2005 12:01:00 AM

0

* 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);}


dblack

2/27/2005 9:45:00 AM

0

Sam Roberts

3/8/2005 4:37:00 AM

0

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