Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby ternary operator if else

How would you make this ternary?

if @user.skill.present?
    @skill = @user.skill
else
    @skill = Skill.new
end 
like image 688
pipinha Avatar asked Mar 31 '26 04:03

pipinha


1 Answers

@skill = @user.skill || Skill.new

If the value of @user.skill if nil, it will asign the next value( Skill.new) to @skill.

like image 151
benja d Avatar answered Apr 02 '26 20:04

benja d