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.
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-19 20: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
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-04-19 20: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
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-04-19 21:02
(Received via mailing list)
Oh wait I think I misunderstood the question are you looking to
overwrite it in the same
class?
49ab3ce5a4922b4747d1d6f330784629?d=identicon&s=25 Jake McArthur (Guest)
on 2006-04-19 21: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
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-19 21:30
(Received via mailing list)
Yeah, not looking to subclass.

On Thursday, April 20, 2006, at 3:59 AM, Farrel Lifson wrote:
>Oh wait I think I misunderstood the question are you looking to
>overwrite it in the same
>class?
>


_Kevin
B44ab09b79ee4a0cc4b4ca69e10eeb3a?d=identicon&s=25 Brian Mitchell (Guest)
on 2006-04-19 21:52
(Received via mailing list)
On 4/19/06, Kevin Olbrich <devlists-ruby-talk@devlists.com> 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.
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-19 23: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 Mitchell 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.