Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created August 5, 2011 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trshafer/1128263 to your computer and use it in GitHub Desktop.
Save trshafer/1128263 to your computer and use it in GitHub Desktop.
Dynamically overwrite methods
class Frog
def self.go
puts 45
end
end
Frog.go
module Whatever
def self.extended(base)
(class << base; self; end).send(:alias_method, :go, :new_go)
end
def new_go
puts 99
end
end
Frog.send(:extend, Whatever)
Frog.go
class Frog
def go
puts 45
end
end
Frog.new.go
module Whatever
def self.included(base)
base.send(:alias_method, :go, :new_go)
end
def new_go
puts 99
end
end
Frog.send(:include, Whatever)
Frog.new.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment