0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[py2rb] not

Last updated at Posted at 2021-12-08

はじめに

移植やってます

not (Python)

if not v:

not vTrueFalse

>>> print(not 0)
True
>>> print(not 1)
False
>>> print(not None)
True
>>> print(not True)
False
>>> print(not False)
True
>>> print(not [])
True
>>> print(not {})
True
>>> print(not "")
True

0""の否定でTrueになるんですね。

! (Ruby)

> puts 0.!
false
> puts 1.!
false
> puts nil.!
true
> puts true.!
false
> puts false.!
true
> puts [].!
false
> puts {}.!

> puts ''.!
false

slackで知ったのですが、.!という書き方が好きですね。
Pythonの否定するオブジェクトによる返り値の変化に対応するのが大変そうです。
それ用の判定メソッドを別途作成するか、オブジェクトをあらかじめ調査しておいて決め打ちするかですよね。
[追記]

[0, nil, false].include?(obj).! && obj.empty?.!

これだとSetを含め、1行でいけそう。

メモ

  • Python の not を学習した
  • 道のりは遠そう
0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?