# 真偽値. 確認用コード

# Bash

...


items=(
-2
-1
0
1
2
)

function check()
{
  return $1
}

for v in "${items[@]}"
do
  b="false"
  if check "$v"; then
    b="true"
  fi
  printf "%-8s is %s\n" "$v" "$b"
done
-2       is false
-1       is false
0        is true
1        is false
2        is false

# PHP

...

foreach ([
true,
// True,
// TRUE,
false,
// False,
// FALSE,
null,
'',
'true',
'false',
'0',
-2,
-1,
0,
1,
2,
[],
[true],
[false],
] as $i => $v) {
    printf("%2d %-10s is %s\n", $i, json_encode($v), ($v ? 'true' : 'false'));
}
 0 true       is true
 1 false      is false
 2 null       is false
 3 ""         is false
 4 "true"     is true
 5 "false"    is true
 6 "0"        is false
 7 -2         is true
 8 -1         is true
 9 0          is false
10 1          is true
11 2          is true
12 []         is false
13 [true]     is true
14 [false]    is true

# Python

...

for [i, v] in enumerate([
  True,
  False,
  None,
  '',
  'True',
  'False',
  "0",
  -2,
  -1,
  0,
  1,
  2,
  [],
  [True],
  [False],
]):
  print "%2d : %-8s is %s" % (i + 1, pprint.pformat(v), 'true' if v else 'false')
 1 : True     is true
 2 : False    is false
 3 : None     is false
 4 : ''       is false
 5 : 'True'   is true
 6 : 'False'  is true
 7 : '0'      is true
 8 : -2       is true
 9 : -1       is true
10 : 0        is false
11 : 1        is true
12 : 2        is true
13 : []       is false
14 : [True]   is true
15 : [False]  is true

# Ruby

...

[
  true,
  false,
  nil,
  '',
  'true',
  'false',
  '0',
  -2,
  -1,
  0,
  1,
  2,
  [],
  [true],
  [false],
].each.with_index(1,) {|v, i|
  puts "%2d : %-8s is %s" % [i, v.inspect, v ? 'true' : 'false']
}
 1 : true     is true
 2 : false    is false
 3 : nil      is false
 4 : ""       is true
 5 : "true"   is true
 6 : "false"  is true
 7 : "0"      is true
 8 : -2       is true
 9 : -1       is true
10 : 0        is true
11 : 1        is true
12 : 2        is true
13 : []       is true
14 : [true]   is true
15 : [false]  is true

[ 📩 ご意見 ]