Skip to content

Instantly share code, notes, and snippets.

@vglebov
Created December 20, 2017 03:17
Show Gist options
  • Save vglebov/76ca216f16c9a24ce716f181a1a710de to your computer and use it in GitHub Desktop.
Save vglebov/76ca216f16c9a24ce716f181a1a710de to your computer and use it in GitHub Desktop.
# Какую операцию нужно поставить вместо <<operation>>, чтобы тест прошел?
def call_unless(a, b)
unless a.nil? <<operation>> b.nil?
:then
else
:else
end
end
describe 'Человеку легко ошибиться с выбором операции в "unless a.nil? operation b.nil? then ... else ... end", ' do
[
[:not_nil, :not_nil, :then],
[nil, :not_nil, :else],
[:not_nil, nil, :else],
[nil, nil, :else],
].each do |a, b, expected|
it "когда a=#{a.inspect} и b=#{b.inspect}, то ожидаем переход по ветке #{expected.inspect}" do
expect(call_unless(a, b)).to eql(expected)
end
end
end
@dsalamau
Copy link

По-моему || ?

@vglebov
Copy link
Author

vglebov commented Dec 20, 2017

Чего гадать, легко проверить

cd /tmp
wget https://gist.githubusercontent.com/vglebov/76ca216f16c9a24ce716f181a1a710de/raw/unless_condition_spec.rb 
sed -i 's/<<operation>>/||/' unless_condition_spec.rb 
rspec unless_condition_spec.rb 

@nozim
Copy link

nozim commented Dec 24, 2017

а лучше вообще не пользоваться unless в сложных условиях. Чем меньше инверсий в условиях тем проще.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment