Searched the archives but didn’t quite find what I was looking for.
Let’s say I have a class:
class Foo
attr_reader :val1, :val2
def initialize @val1 = “hello” @val2 = "world"
end
end
Then, let’s say I have a subclass of Foo, and I want to remove the
"val1" method. How would I do it?
class Bar < Foo
remove_method(:val1) # Fails, says ‘val1’ is not defined in Bar
(?)
self.remove_method(:val1) # Fails, says private method
class << self
remove_method(:val1) # Fails, says ‘val1’ is not defined in
Class
end
end
I also tried this within Bar’s initialize() method with no luck.
class Foo
attr_reader :val1, :val2
def initialize @val1 = “hello” @val2 = “world”
end
end
Then, let’s say I have a subclass of Foo, and I want to remove the
“val1” method. How would I do it?
Well there’s always:
class Bar
def val1
raise NameError
end
end
i.e. replace val1 an explicit ‘I do not exist’ method. Otherwise it will
be
inherited.
Wouldn’t it be better to do
class Bar
def val1
method_missing :val1
end
end
This would delegate to the default handling of missing methods. This would
keep the scheme if there was another sub class of Bar that want’s to deal
with all missing method by itself.
Regards
robert
···
On Thu, Jun 05, 2003 at 01:57:32AM +0900, Daniel Berger wrote:
Two forms of cartesian product (one returning a set of immutable tuples
and the other returning a set of arrays) – I plan on looking at using
the cartesian product method in enum tools to implement these.
‘unique_attributes’ method – Deletes objects with duplicate attributes
from the calling set. Used to deal with a set being able to contain two
different set objects that have the same elements.
‘relation?’ method.
‘powerset?’ method – Determines if the calling set is a powerset of
the given set. I’m still working on the ‘make_powerset’ method.
‘make_complement’ and ‘complement?’ methods.
Comments, suggestions, revisions, ideas, derisive laughter, etc.
welcome.
Well, I don’t know exactly It doesn’t define a dummy method if one does
not already exist:
irb(main):001:0> class A
irb(main):002:1> undef_method :val1
irb(main):003:1> end
NameError: undefined method val1' for class A’
from (irb):2:in `undef_method’
from (irb):2
Regards,
Brian.
···
On Thu, Jun 05, 2003 at 05:36:00PM +0900, ts wrote:
Well, I don’t know exactly It doesn’t define a dummy method if one
does
not already exist:
Do you think that would make sense?
robert
···
On Thu, Jun 05, 2003 at 05:36:00PM +0900, ts wrote:
irb(main):001:0> class A
irb(main):002:1> undef_method :val1
irb(main):003:1> end
NameError: undefined method val1' for class A’
from (irb):2:in `undef_method’
from (irb):2