Last active
August 29, 2015 14:13
-
-
Save anicholson/083580efa77ee04cb39b to your computer and use it in GitHub Desktop.
Do refinements apply only to instance methods?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Traveller | |
def what_are_you | |
puts "I'm a Backpacker" | |
end | |
def self.preferred_accommodation | |
puts "Hostels" | |
end | |
end | |
module Refinements | |
module Money | |
module InstanceMethods | |
def what_are_you | |
puts "I'm a cashed-up hedonist!" | |
end | |
end | |
module ClassMethods | |
def preferred_accommodation | |
puts "Expensive Hotels" | |
end | |
end | |
end | |
refine(Traveller) { include Money::InstanceMethods } | |
refine(Traveller.singleton_class) { include Money::ClassMethods } | |
end | |
def unrefined | |
Traveller.new.what_are_you # => I'm a Backpacker | |
Traveller.preferred_accommodation # => Hostels | |
end | |
module Context | |
using Refinements | |
def self.refined | |
Traveller.new.what_are_you # => I'm a cashed-up hedonist! | |
Traveller.preferred_accommodation # => Expensive Hotels | |
end | |
end | |
Context.refined | |
unrefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment