Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Can I remove methods from a single object, but not its class?

Reply
Thread Tools

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

 
 
Sam Roberts
Guest
Posts: n/a
 
      02-26-2005
I want to allow method calls during initialization, but not after:

class Foo
attr_writer 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



 
Reply With Quote
 
 
 
 
Nikolai Weibull
Guest
Posts: n/a
 
      02-27-2005
* Sam Roberts (Feb 27, 2005 00:50):
> class Foo
> attr_writer 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 pt
def initialize
yield self
undef 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);}


 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      02-27-2005
Hi --

On Sun, 27 Feb 2005, Sam Roberts wrote:

> I want to allow method calls during initialization, but not after:
>
> class Foo
> attr_writer 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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Sam Roberts
Guest
Posts: n/a
 
      03-08-2005
Thank you both!

Sam

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


Quoting (E-Mail Removed), on Sun, Feb 27, 2005 at 06:45:24PM +0900:
> Try:
>
> class << self
> undef_method 'opt='
> end
>
> David



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
BoundColumn single DataBind equiv but not in a single row? Randall Parker ASP .Net 1 12-12-2005 04:11 PM



Advertisments