Listening for Changes to a Class : method_added « Reflection « Ruby






Listening for Changes to a Class


class Tracker
  def important
    "This is an important method!"
  end

  def self.method_added sym
    if sym == :important
      raise 'The "important" method has been redefined!'
    else
      puts %{Method "#{sym}" was (re)defined.}
    end
  end

  def self.method_removed sym
    if sym == :important
      raise 'The "important" method has been removed!'
    else
      puts %{Method "#{sym}" was removed.}
   end
  end

  def self.method_undefined sym
    if sym == :important
      raise 'The "important" method has been undefined!'
    else
      puts %{Method "#{sym}" was removed.}
    end
  end
end

class Tracker
  def new_method
    'This is a new method.'
  end
end
# Method "new_method" was (re)defined.

class Tracker
  undef :important
end
# RuntimeError: The "important" method has been undefined!

class Tracker
  include Enumerable
end

# Nothing!

 








Related examples in the same category