8
9

More than 5 years have passed since last update.

レビュー時の勘所1 if xxx.present?〜 → Object#try(ブロック渡し)

Posted at

よくやるかんじの書き方

first_user_fullname = ""
if group.users.first.present?
  first_user = group.users.first
  first_user_fullname = first_user.last_name + " " + first_user.first_name
else
 first_user_fullname = "名無しさん"
end

これはこんなかんじに書き換えられる

first_user_fullname = group.users.first.try do |first_user|
  first_user.last_name + " " + first_user.first_name
end
first_user_fullname ||= "名無しさん"
8
9
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
8
9