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

S

Sam Roberts

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
 
N

Nikolai Weibull

* 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
 
D

David A. Black

Hi --

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
 
S

Sam Roberts

Thank you both!

Sam

Quoting (e-mail address removed), 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 (e-mail address removed), on Sun, Feb 27, 2005 at 06:45:24PM +0900:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top