Forum: Ruby overriding a class method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kevin O. (Guest)
on 2006-04-19 22:50
(Received via mailing list)
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
Farrel L. (Guest)
on 2006-04-19 22:56
(Received via mailing list)
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
Farrel L. (Guest)
on 2006-04-19 23:02
(Received via mailing list)
Oh wait I think I misunderstood the question are you looking to
overwrite it in the same
class?
Jake McArthur (Guest)
on 2006-04-19 23:09
(Received via mailing list)
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
Kevin O. (Guest)
on 2006-04-19 23:30
(Received via mailing list)
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
Brian M. (Guest)
on 2006-04-19 23:52
(Received via mailing list)
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.
Kevin O. (Guest)
on 2006-04-20 01:04
(Received via mailing list)
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
This topic is locked and can not be replied to.