Remove_method question

Hi all,

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.

Any ideas?

Dan

Then, let's say I have a subclass of Foo, and I want to remove the
"val1" method. How would I do it?

You can remove the method only in the class where this method is defined
(in Foo in your case)

class Bar < Foo
   remove_method(:val1)

Is this was valid then you'll have problem with

class Foo
   attr_reader :val1, :val2
   def initialize
      @val1 = "hello"
      @val2 = "world"
   end
end

class A < Foo
end

class Bar < Foo
   remove_method(:val1)
end

#val1 will also be removed for A

Use undef_method if you want just to modify Bar

Guy Decoux

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.

Regards,

Brian.

···

On Thu, Jun 05, 2003 at 01:57:32AM +0900, Daniel Berger wrote:

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?

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030604181446.GA1523@uk.tiscali.com

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?

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:

I’ve added some additional draft set methods to the Ruby Garden Wiki at

http://www.rubygarden.org/ruby?AdditionalSetMethods

Methods added are:

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.

Regards,

Mark

Good idea. In which case, to be properly useful:

class Bar
def val1(*args,&block)
method_missing(:val1,*args,&block)
end
end

Cheers,

Brian.

···

On Thu, Jun 05, 2003 at 05:16:40AM +0900, Robert wrote:

Wouldn’t it be better to do

class Bar
def val1
method_missing :val1
end
end

  class Bar
    def val1(*args,&block)
      method_missing(:val1,*args,&block)

What do you think that undef_method do ?

svg% cat b.rb
#!/usr/bin/ruby
def method_missing(id, *args)
   p "method_missing #{id}"
end

class A
   def val1; end
end

class B < A
   undef_method :val1
end

B.new.val1
svg%

svg% b.rb
"method_missing val1"
svg%

Guy Decoux

Well, I don’t know exactly :slight_smile: 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:

What do you think that undef_method do ?

Well, I don't know exactly :slight_smile: It doesn't define a dummy method if one does
not already exist:

It define a dummy method, but ruby know this :-)) This is why it give an
error in your case

Guy Decoux

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030605114827.GA2687@uk.tiscali.com

What do you think that undef_method do ?

Well, I don’t know exactly :slight_smile: It doesn’t define a dummy method if one
does
not already exist:

Do you think that would make sense? :slight_smile:

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

Regards,

Brian.

Not really - just pointing out that it’s being a bit cleverer than what I
had proposed. It’s clearly the Right Solution here.

Cheers,

Brian.

···

On Thu, Jun 05, 2003 at 09:38:11PM +0900, Robert Klemme wrote:

Well, I don’t know exactly :slight_smile: It doesn’t define a dummy method if one
does
not already exist:

Do you think that would make sense? :slight_smile:

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030605124559.GA3746@uk.tiscali.com

Well, I don’t know exactly :slight_smile: It doesn’t define a dummy method if
one
does
not already exist:

Do you think that would make sense? :slight_smile:

Not really - just pointing out that it’s being a bit cleverer than what
I
had proposed. It’s clearly the Right Solution™ here.
^^^^

Cheers,

Brian.

:-))

robert
···

On Thu, Jun 05, 2003 at 09:38:11PM +0900, Robert Klemme wrote: