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
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
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, Chris T. [email protected] wrote:
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
and then give you the answer
(I’m using ruby 1.8.6.)
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.
We’re on a mission from God. ~ Elwood,
El Jul 22, 2007, a las 6:44 PM, Dean W.
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)
– fxn
On 7/22/07, Robert D. [email protected] wrote:
Let me plead you not to top post first
and then give you the answer
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/23/07, Chris T. [email protected] wrote:
On 7/22/07, Robert D. [email protected] wrote:
Let me plead you not to top post first
and then give you the answerWhat’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
Robert D. wrote:
On 7/23/07, Chris T. [email protected] wrote:
On 7/22/07, Robert D. [email protected] wrote:
Let me plead you not to top post first
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.
Hi –
On Mon, 23 Jul 2007, Xavier N. wrote:
El Jul 22, 2007, a las 6:44 PM, Dean W. 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 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
Hi –
On Mon, 23 Jul 2007, Dean W. wrote:
exist.
endOnce 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 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
David
On 7/22/07, [email protected] [email protected] wrote:
exist.
endOnce 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs