Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active January 4, 2016 13:19
Show Gist options
  • Select an option

  • Save zeroeth/8626790 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/8626790 to your computer and use it in GitHub Desktop.
include override
module SwingableInstanceMethods
def instance_swing
puts 'Did an instance swing!'
end
end
module SwingableClassMethods
def static_swing
puts 'Did a static swing!'
end
def include?(xyz)
puts "XYZ"
super # arguments are implicit
end
end
class BaseballBat
include SwingableInstanceMethods
extend SwingableClassMethods
end
class SuperBat < BaseballBat
end
class OtherThing
end
BaseballBat.static_swing
BaseballBat.new.instance_swing
puts BaseballBat.include? SwingableInstanceMethods
puts SuperBat.include? SwingableInstanceMethods
puts OtherThing.include? SwingableInstanceMethods
module Swingable
def include?(xyz)
puts "XYZ"
super # arguments are implicit
end
end
class BaseballBat
extend Swingable
end
class SuperBat < BaseballBat
end
class OtherThing
end
puts BaseballBat.include? Swingable
puts SuperBat.include? Swingable
puts OtherThing.include? Swingable
# no such thing...
puts BaseballBat.extend? Swingable
module Swingable
def self.included(base)
base.extend(ClassMethods)
end
def instance_swing
puts 'Did an instance swing!'
end
module ClassMethods
def static_swing
puts 'Did a static swing!'
end
def include?(xyz)
puts "XYZ"
super # arguments are implicit
end
end
end
class BaseballBat
include Swingable
end
class SuperBat < BaseballBat
end
class OtherThing
end
BaseballBat.static_swing
BaseballBat.new.instance_swing
puts BaseballBat.include? Swingable
puts SuperBat.include? Swingable
puts OtherThing.include? Swingable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment