Skip to content

Instantly share code, notes, and snippets.

@gaspard
Created September 13, 2009 15:57
Show Gist options
  • Save gaspard/186219 to your computer and use it in GitHub Desktop.
Save gaspard/186219 to your computer and use it in GitHub Desktop.
module Redef
def foo
puts "'foo' from module"
end
end
class A
define_method("foo") do
puts "'foo' from define_method"
end
include Redef
end
A.new.foo
# ===> 'foo' from define_method
# why not "'foo' from module" ?
# David's example
class A
def foo
puts "'foo' from A"
end
include Redef
end
A.new.foo
# ===> 'foo' from A
=begin
David A. Black says:
It's a matter of the order of method lookup. In general, an object
looks for a method first in its class, and then in modules mixed into
that class.
------
7stud says:
By the way, you can get the lookup order using the ancestors method:
A.ancestors
# ===> [A, Redef, Object, Kernel]
Does not list singleton class though:
class A
def initialize
extend Redef
end
end
A.ancestors
# ===> [A, Object, Kernel]
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment