Can you override an aliased method in Ruby?
Just re-establish the alias:
class Foo def bar "bar" end alias :saloon :barendclass Foo def bar "BAR" end alias :saloon :barendputs Foo.new.saloon # => "BAR"
class Foo def bar "bar" end def saloon bar endend
This is not an alias at all, but it works as you want.
Here is another answer, but you have to do some additional steps: collect the aliases before overriding, and realias after:
class Class def get_aliases method_name original_proc = instance_method method_name aliases = [] instance_methods.each do |meth| # if the methods have different names but they're the same, they're aliased if meth != method_name.to_s && original_proc == instance_method(meth) aliases << meth end end aliases endendclass Foo def bar "bar" end alias :saloon :barendclass Foo aliases = get_aliases :bar def bar "BAR" end aliases.each { |a| alias_method a, :bar }endputs Foo.new.saloon #=> BAR
BTW, if anyone can strip off one of that steps, may I know it! :)