rspec - Store ruby code in a let() variable -
i want store code run inside each_with_index
block:
describe '#each_with_index' subject { [:a,:b].each_with_index{ block.to_ruby } } context 'when block prints v console' let(:block) { '|v| puts v' } specify { expect { subject }.to output(':a :b').to_stdout } end context 'when block prints console' let(:block) { '|v,i| puts i' } specify { expect { subject }.to output('0 1').to_stdout } end context 'when block prints v , console' let(:block) { '|v,i| puts "#{v} , {i}"' } specify { expect { subject }.to output(':a , 0 :b , 1').to_stdout } end end
i don't need code stored string, way show mean. want in-block code, pipes, , everything. i've got feeling can use proc.new
pipes tripping me up.
something like:
let(:block) { proc.new{ |v| puts v } } subject { [:a,:b].each_with_index { |*args| block.call args } }
Comments
Post a Comment