Skip to content

Instantly share code, notes, and snippets.

@zampino
Last active January 2, 2016 17:39
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 zampino/8338123 to your computer and use it in GitHub Desktop.
Save zampino/8338123 to your computer and use it in GitHub Desktop.
Minimal example about ruby 2.1.0 refinements
module MyRefinements
refine Object do
def nice_name
"wow I am a #{self.class}"
end
end
refine Hash do
def foo
"foo"
end
def empty?
delete(:null)
super
end
end
end
class MyClass
using MyRefinements
attr_reader :registry
def nice_name; super; end
def initialize(reg=Hash.new)
@registry = reg
end
def reg_foo
registry.foo
end
def empty?
registry.empty?
end
end
describe MyClass do
it "should have a nice name" do
expect(subject.nice_name).to eq "wow I am a MyClass"
end
let(:its_registry) { subject.registry }
specify "our class can use refined classeso" do
expect(subject.reg_foo).to eq("foo")
end
specify "but the refinement doesn't persist externally" do
expect { its_registry.foo }.to raise_error(NoMethodError)
end
context "using super call in refinements" do
subject { MyClass.new({ null: true}) }
specify { expect(subject).to be_empty }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment