Last active
January 2, 2016 17:39
-
-
Save zampino/8338123 to your computer and use it in GitHub Desktop.
Minimal example about ruby 2.1.0 refinements
This file contains hidden or 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
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