With Ruby, how can I set a variable based on a condition? Something like:
dog_name = params[:dog][:name] if params[:dog]
And then dog_name just wouldn’t be set if params[:dog] wasn’t set.
Right now I’m getting an error if params[:dog] is nil.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use the
&&operator:So now
dog_namewill benilifparams[:dog]does not exist, else it will be the value ofparams[:dog][:name].