How do you "undef_method" for a class method?

Module#undef_method and Module#remove_method work fine for instance methods,
but if you specify a class method, your get an error that it doesn't exist.
(I'm using ruby 1.8.6.)

Is there a way to do this? TIA.

Dean

···

--
Dean Wampler
http://www.objectmentor.com
http://www.aspectprogramming.com

Well, not sure if there's a more elegant approach to this (and there
probably is) but one way to do it would be to open up the class itself and
redfine the method to do nothing or raise an exception. I'm curious to see
if there *is* another way to do this, though.

Chris

···

On 7/22/07, Dean Wampler <deanwampler@gmail.com> wrote:

Module#undef_method and Module#remove_method work fine for instance
methods,
but if you specify a class method, your get an error that it doesn't
exist.
(I'm using ruby 1.8.6.)

Is there a way to do this? TIA.

Dean

--
Dean Wampler
http://www.objectmentor.com
http://www.aspectprogramming.com
http://www.contract4j.org

You need to get into the singleton class:

   (class << Foo; self; end).send(:remove_method, :method)

-- fxn

···

El Jul 22, 2007, a las 6:44 PM, Dean Wampler escribió:

Module#undef_method and Module#remove_method work fine for instance methods,
but if you specify a class method, your get an error that it doesn't exist.
(I'm using ruby 1.8.6.)

Well, not sure if there's a more elegant approach to this (and there
probably is) but one way to do it would be to open up the class itself and
redfine the method to do nothing or raise an exception. I'm curious to see
if there *is* another way to do this, though.

Let me plead you not to top post first :wink:
and then give you the answer

Chris

>
> Module#undef_method and Module#remove_method work fine for instance
> methods,
> but if you specify a class method, your get an error that it doesn't
> exist.
> (I'm using ruby 1.8.6.)
>
> Is there a way to do this? TIA.
>
> Dean
>
> --
> Dean Wampler
> http://www.objectmentor.com
> http://www.aspectprogramming.com
> http://www.contract4j.org
>

505/5 > cat remove_method.rb && ruby remove_method.rb
class A
  def A.hi
    "hello"
  end
end

p A.hi
class << A
  remove_method :hi
end
p A.hi
"hello"
remove_method.rb:11: undefined method `hi' for A:Class (NoMethodError)
robert@PC:~/log/ruby/ML 19:46:46

If memory serves you should not use undefine_method as it leaks.

HTH
Robert

···

On 7/22/07, Chris Thiel <ccthiel@gmail.com> wrote:

On 7/22/07, Dean Wampler <deanwampler@gmail.com> wrote:

--

We're on a mission from God. ~ Elwood,

Hi --

Module#undef_method and Module#remove_method work fine for instance methods,
but if you specify a class method, your get an error that it doesn't exist.
(I'm using ruby 1.8.6.)

You need to get into the singleton class:

(class << Foo; self; end).send(:remove_method, :method)

You're doing too much work :slight_smile: See Robert D.'s answer:

   class << Foo
     remove_method :method
   end

Once you're in the class definition body, you can just go ahead and do
things; you don't have to grab the class and address it
programmatically unless there's a specific reason.

David

···

On Mon, 23 Jul 2007, Xavier Noria wrote:

El Jul 22, 2007, a las 6:44 PM, Dean Wampler escribió:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

What's that supposed to mean anyhow?
I was just trying to help...which if I'm not mistaken, is the whole point of
this list.

···

On 7/22/07, Robert Dober <robert.dober@gmail.com> wrote:

Let me plead you not to top post first :wink:
and then give you the answer

>

Hi --

>
>> Module#undef_method and Module#remove_method work fine for instance
>> methods,
>> but if you specify a class method, your get an error that it doesn't
exist.
>> (I'm using ruby 1.8.6.)
>
> You need to get into the singleton class:
>
> (class << Foo; self; end).send(:remove_method, :method)

You're doing too much work :slight_smile: See Robert D.'s answer:

   class << Foo
     remove_method :method
   end

Once you're in the class definition body, you can just go ahead and do
things; you don't have to grab the class and address it
programmatically unless there's a specific reason.

That did it. Thanks. It didn't occur to me to open the singleton class on a
class itself, as opposed to opening the singleton class for an object. I
assume that both are
called singleton classes (?).

dean

David

···

On 7/22/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

On Mon, 23 Jul 2007, Xavier Noria wrote:
> El Jul 22, 2007, a las 6:44 PM, Dean Wampler escribió:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

--
Dean Wampler
http://www.objectmentor.com
http://www.aspectprogramming.com
http://www.contract4j.org

>
> Let me plead you not to top post first :wink:
> and then give you the answer
>
> >
>
What's that supposed to mean anyhow?

This is a bottom post, you posted at the end of the message you respond to,
99% of us prefer this if possible. Your first post was on top of the message.
It is completely standard to make the remark I made, and normally
nobody takes offense. Maybe there is something wrong with the applied
verb, "to plead" I thought it means "to ask strongly", if I offended
you by a bad choice of language I apologize.
On my behalf I thought that "What's that supposed to mean anyhow" is
quite bad, but surely I am mistaken.

I was just trying to help...which if I'm not mistaken, is the whole point of
this list.

No I would not reduce it to that, but it is an important point and I
do not think that I said you did not, did I?

Cheers and sorry for my bad English.

Robert

···

On 7/23/07, Chris Thiel <ccthiel@gmail.com> wrote:

On 7/22/07, Robert Dober <robert.dober@gmail.com> wrote:

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Hi --

···

On Mon, 23 Jul 2007, Dean Wampler wrote:

On 7/22/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Mon, 23 Jul 2007, Xavier Noria wrote:

> El Jul 22, 2007, a las 6:44 PM, Dean Wampler escribió:
>
>> Module#undef_method and Module#remove_method work fine for instance
>> methods,
>> but if you specify a class method, your get an error that it doesn't
exist.
>> (I'm using ruby 1.8.6.)
>
> You need to get into the singleton class:
>
> (class << Foo; self; end).send(:remove_method, :method)

You're doing too much work :slight_smile: See Robert D.'s answer:

   class << Foo
     remove_method :method
   end

Once you're in the class definition body, you can just go ahead and do
things; you don't have to grab the class and address it
programmatically unless there's a specific reason.

That did it. Thanks. It didn't occur to me to open the singleton class on a
class itself, as opposed to opening the singleton class for an object. I
assume that both are called singleton classes (?).

By me they are :slight_smile: Sometimes you'll hear the singleton class of a
Class object referred to as a metaclass. Then again, sometimes you'll
hear singleton classes in general referred to as metaclasses. I'm
happy with "singleton class", but we're all waiting for a
pronouncement from Matz :slight_smile:

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Robert Dober wrote:

>
>
> Let me plead you not to top post first :wink:
> and then give you the answer

This is a bottom post, you posted at the end of the message you respond
to,
99% of us prefer this if possible. Your first post was on top of the
message.

Cheers and sorry for my bad English.

Robert

oh wow,
I didn't know about this.

I read it and thought "what does top posting mean?"
does it mean, "you shouldn't be the first to respond unless you have a
good answer"

but cool,
I'll be mindful of this in the future.

···

On 7/23/07, Chris Thiel <ccthiel@gmail.com> wrote:

On 7/22/07, Robert Dober <robert.dober@gmail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.