Skip to content

Instantly share code, notes, and snippets.

@aergonaut
Created April 21, 2020 06:27
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 aergonaut/44902d86193fc4135494940278689cde to your computer and use it in GitHub Desktop.
Save aergonaut/44902d86193fc4135494940278689cde to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activesupport", "~> 4.2"
end
require "active_support/all"
class Dog
def speak
"woof!"
end
end
module British
def speak
"#{super} eh, wot?"
end
end
class BritishDog < Dog
prepend British
end
puts "only prepend: #{BritishDog.new.speak}"
class Cat
def speak
"meow!"
end
end
module Pirate
def self.included(base)
base.class_eval do
alias_method_chain :speak, :pirate_accent
end
end
def speak_with_pirate_accent
"#{speak_without_pirate_accent} yarr!"
end
end
class PirateCat < Cat
include Pirate
end
puts "only a_m_c: #{PirateCat.new.speak}"
class Duck
def speak
"quack!"
end
end
class PirateBritishDuck < Duck
include Pirate
prepend British
end
puts "a_m_c then prepend: #{PirateBritishDuck.new.speak}"
class Bear
def speak
"growl!"
end
end
class BritishPirateBear < Bear
prepend British
include Pirate
end
puts "prepend then a_m_c: #{BritishPirateBear.new.speak}"
@aergonaut
Copy link
Author

Looks like I independently rediscovered something NewRelic discovered ages ago https://gist.github.com/kenichi/f251b30c7c852b5e035a903754e2434d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment