阅读文章http://jeffkreeftmeijer.com/2011/method-chaining-and-lazy-evaluation-in-ruby/后,我开始寻找方法链和懒惰评估的更好解决方案.
我想我已经用以下五个规格封装了核心问题; 谁能让他们全部通过?
一切顺利:子类化,委托,元编程,但对后者不鼓励.
将依赖关系保持在最低限度是有利的:
require 'rspec'
class Foo
# Epic code here
end
describe Foo do
it 'should return an array corresponding to the reverse of the method chain' do
# Why the reverse? So that we're forced to evaluate something
Foo.bar.baz.should == ['baz', 'bar']
Foo.baz.bar.should == ['bar', 'baz']
end
it 'should be able to chain a new method after initial evaluation' do
foobar = Foo.bar
foobar.baz.should == ['baz', 'bar']
foobaz = Foo.baz
foobaz.bar.should == ['bar', 'baz'] …Run Code Online (Sandbox Code Playgroud)