(Disclaimer - I've never written Ruby, but I'm assuming || is logical or - a || b is a if a is true, otherwise b)
I believe that ||= might not work very well if you try to memoize false.
The point of memoization is that once a value is set, you don't change it:
it 'smilers never "CRY"' do
expect(smile.smile).to be_nil
expect(smile.ensmile).to eq 'SMILE'
expect(smile.ensmile(cry: true)).to eq 'SMILE'
expect(smile.smile).to eq 'SMILE'
end
it 'criers never "SMILE"' do
expect(smile.smile).to be_nil
expect(smile.ensmile(cry: true)).to eq 'CRY'
expect(smile.ensmile).to eq 'CRY'
expect(smile.smile).to eq 'CRY'
end
Consider a boolean property instead:
class Smile
attr_accessor :smiling
def ensmile(cry: false)
@smiling ||= begin
!cry
end
end
end
And test that criers never smile:
it 'criers never "SMILE"' do
expect(smile.smiling).to be_nil
expect(smile.ensmile(cry: true)).to eq false
expect(smile.ensmile).to eq false # Failure here
expect(smile.smiling).to eq false # And here, for the same reason
end
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
(Disclaimer - I've never written Ruby, but I'm assuming || is logical or - a || b is a if a is true, otherwise b)
I believe that ||= might not work very well if you try to memoize
false
.The point of memoization is that once a value is set, you don't change it:
Consider a boolean property instead:
And test that criers never smile: