Skip to content

Instantly share code, notes, and snippets.

@anicholson
Last active August 29, 2015 14:13
Show Gist options
  • Save anicholson/083580efa77ee04cb39b to your computer and use it in GitHub Desktop.
Save anicholson/083580efa77ee04cb39b to your computer and use it in GitHub Desktop.
Do refinements apply only to instance methods?
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