LoginSignup
0
0

More than 5 years have passed since last update.

unlessの評価がいつとまるのか慣れてないので苦戦すると思って冷静に試してみると...

Last updated at Posted at 2016-10-03

全部ORならtrueが来たら評価がとまって実行されない

def hoge
  puts :hoge
  false
end

def foo
  puts :foo
  true
end

def bar
  puts :bar
  true
end

unless hoge || foo || bar
  puts :a
end

結果:

hoge
foo

全部ANDならfalseが来たら評価がとまって実行される

def hoge
  puts :hoge
  true
end

def foo
  puts :foo
  false
end

def bar
  puts :bar
  true
end

unless hoge && foo && bar
  puts :a
end

結果:

hoge
foo
a

よくよく見ると評価がとまるのifのときと同じか。。。

とはいえ、ややこしいので ベストプラクティスとしては、 unless&&||を使ってはいけない。混乱しやすい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0