Calling super in overwritten methods

J

Joerg Diekmann

Hi - not sure if this is possible - but it feels like it could be with
some serious ruby-fu.

I have the following:

class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
super
end

def method2
return 200
end
end


This is what happens:

b.method1 # 200

But, what I want is:

b.method1 # 100


Is this possible at all?

Thanks
Joerg
 
J

Joerg Diekmann

Ok pity there's no simple Ruby solution to it. I'll just have to rename
my methods in my derived class then. Thanks though!
 
H

hemant

Hi - not sure if this is possible - but it feels like it could be with
some serious ruby-fu.

I have the following:

class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
super
end

def method2
return 200
end
end


This is what happens:

b.method1 # 200

But, what I want is:

b.method1 # 100


Is this possible at all?

Thanks
Joerg

Indeed but sometime back, we had similar discussion here and someone
(David I guess) proposed:

class Parent
def knox
puts 'parent'
end
end

class Child < Parent
def knox
puts 'child'
end
def test
self.class.superclass.instance_method( :knox ).bind( self ).call
end
end

Child.new.test
 
J

Joerg Diekmann

Hmmm ... that doesn't work for me.


class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
self.class.superclass.instance_method( :method1 ).bind( self ).call
end

def method2
return 200
end
end

b.method1 # Still 200 instead of 100
 
T

Trans

Joerg said:
Hi - not sure if this is possible - but it feels like it could be with
some serious ruby-fu.

I have the following:

class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
super
end

def method2
return 200
end
end


This is what happens:

b.method1 # 200

But, what I want is:

b.method1 # 100


Is this possible at all?

Local methods would do the trick. We don't have those. But here's a
cool way:

require 'facets'
require 'kernel/as'

class A

def method1
as(A).method2
end

def method2
return 100
end

end

class B < A
def method1
super
end

def method2
return 200
end
end

T.

(http://facets.rubyforge.org)
 
A

ara.t.howard

Hi - not sure if this is possible - but it feels like it could be with
some serious ruby-fu.

I have the following:

class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
super
end

def method2
return 200
end
end


This is what happens:

b.method1 # 200

But, what I want is:

b.method1 # 100


Is this possible at all?

harp:~ > cat a.rb
class A
CLASS = self
def method1 *a, &b
CLASS.instance_method:)method2).bind(self).call *a, &b
end
def method2
return 100
end
end

class B < A
CLASS = self
def method1
super
end
def method2
return 200
end
end

p A.new.method1
p B.new.method1


harp:~ > ruby a.rb
100
100


just because you can, however, doesn't mean you should. why do you want to do this?

-a
 
M

micathom

You didn't say if you can change class a if so you should probably
start the redesign there before resorting to vodoo.

= class A
= def method1
* method2_A
= end
=
= def method2
= return 100
= end
+ alias :method2_A :method2
= end
=
= class B < A
= def method1
= super
= end
=
= def method2
= return 200
= end
+ alias :method2_B :method2
= end

or so. This makes your intentions quite clear I'd say.
 
B

Brian

Hey,
Assuming your trying to make a determination in class B as to which
method to use, you could make method2 a class method.

class A
def method1
A.method2
end

def self.method2
return 100
end
end

class B < A
def method1
x = 1
puts A.method2 if x == 1 # => 100
puts method2 if x == 2 # => 200
end

def method2
return 200
end
end

B.new.method1

- Brian
 
D

David Vallner

--------------enig38569DD8299FAF174ADF42A3
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Joerg said:
Ok pity there's no simple Ruby solution to it. I'll just have to rename= =20
my methods in my derived class then. Thanks though!
=20
=20

Having a "simple" way would be way too C++, since it -is- horribly
breaking object-oriented behaviour. Might as well bring back "virtual".

The unbound method Ruby fu is just right in my opinion, it makes it
instantly clear that the code isn't behaving polymorphically, but uses
the class as a function namespace.

David Vallner


--------------enig38569DD8299FAF174ADF42A3
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFeebby6MhrS8astoRAi/YAJ4nKIEuH4Srjnl3joxpneb59AL7cgCfVTmE
MPtqgFvcsfYEZ867I9SRkyU=
=aNah
-----END PGP SIGNATURE-----

--------------enig38569DD8299FAF174ADF42A3--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top