LoginSignup
6
6

More than 5 years have passed since last update.

【論理演算子】||のデフォルト値を与えるイディオム

Last updated at Posted at 2013-01-15

論理演算子の性質を利用した変数にデフォルト値を与える、よくある書き方。

name = "Ruby"
if var != nil
 name = var
end

が、1行で書くことができる。

name = var || "Ruby"

例 OmniAuthにて

user = User.find_by_provider_and_uid(auth["provider"], auth["uid"])
user ||= User.create_with_omniauth(auth)
6
6
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
6
6