Overriding a class method


#1

Here’s a quick question for the Meta-programming gurus

start with

class A
def self.method_a
puts “A”
end
end

How would I override method_a in such a way that the original method_a
could be called at the end? sort of like that way you can call ‘super’
from a subclass.

_Kevin


#2

super still works:

irb(main):001:0> class A
irb(main):002:1> def self.method_a
irb(main):003:2> puts “A”
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> class NewA < A
irb(main):007:1> def self.method_a
irb(main):008:2> puts “NewA”
irb(main):009:2> super
irb(main):010:2> end
irb(main):011:1> end
=> nil
irb(main):012:0> NewA.method_a
NewA
A

Farrel


#3

I’m not a guru, but the following works for me, though it is not very
elegant:

class A
def self.method_a
puts “A”
end
end

class A
@@old_method_a = self.method(:method_a)
def self.method_a
puts “B”
@@old_method_a.call
end
end

A.method_a # outputs “B”, then “A”

  • Jake McArthur

#4

Oh wait I think I misunderstood the question are you looking to
overwrite it in the same
class?


#5

Yeah, not looking to subclass.

On Thursday, April 20, 2006, at 3:59 AM, Farrel L. wrote:

Oh wait I think I misunderstood the question are you looking to
overwrite it in the same
class?

_Kevin


#6

On 4/19/06, Kevin O. removed_email_address@domain.invalid wrote:

Yeah, not looking to subclass.

class A
def self.meth_a
puts “one”
end
end

class A
class << self; alias meth_a_old meth_a end
def self.meth_a
meth_a_old
puts “two”
end
end

Though I personally think use of alias for this sort of thing as a
“hack” (just my opinion of course). Another technique is to use a
lexical closure:

class A
def self.meth_a
puts “one”
end
end

class A
old_a = method(:meth_a)

The ugly part. This usually is nicer for instance methods.

You could write this a few different ways but they are all

ugly. Possibly something to factor out as a method.

sclass = class << self; self end
sclass.send :define_method, :meth_a do
old_a.call
puts “two”
end
end

Both are messy but one is less likely to collide or cause problems
with other code (not to mention littering the class with aliases).

Brian.


#7

Thanks!

I am slowly but surely getting the hang of this metaprogramming thing.

On Thursday, April 20, 2006, at 4:49 AM, Brian M. wrote:

class << self; alias meth_a_old meth_a end
class A
sclass = class << self; self end

_Kevin