diff --git a/lib/pry/config/memoized_value.rb b/lib/pry/config/memoized_value.rb index 670f45b9..339f6e2b 100644 --- a/lib/pry/config/memoized_value.rb +++ b/lib/pry/config/memoized_value.rb @@ -19,11 +19,15 @@ class Pry class MemoizedValue def initialize(&block) @block = block + @called = false @call = nil end def call - @call ||= @block.call + return @call if @called + + @called = true + @call = @block.call end end end diff --git a/spec/config/memoized_value_spec.rb b/spec/config/memoized_value_spec.rb index 22e278dc..9b8ab864 100644 --- a/spec/config/memoized_value_spec.rb +++ b/spec/config/memoized_value_spec.rb @@ -2,10 +2,20 @@ RSpec.describe Pry::Config::MemoizedValue do describe "#call" do - subject { described_class.new { rand } } - it "memoizes the result of call" do - expect(subject.call).to eq(subject.call) + instance = described_class.new { rand } + expect(instance.call).to eq(instance.call) + end + + it "doesn't conflate falsiness with unmemoizedness" do + count = 0 + instance = described_class.new do + count += 1 + nil + end + expect(instance.call).to eq nil + expect(instance.call).to eq nil + expect(count).to eq 1 end end end