Javier Valencia
Javier Valencia

Reputation: 695

Ruby when nil or empty string

I have RoR app, and I want to change some lines of my code to be more elegant.

foo = params[:customer][:language].nil? or params[:customer][:language].empty? ? 'es' : params[:customer][:language]

I try with

foo = params[:customer][:language] || 'es'

But it's not the same exactly.

Thanks in advance.

Upvotes: 2

Views: 1101

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51181

You can use activesupport's Object#presence method, like this:

foo = params[:customer][:language].presence || 'es'

Upvotes: 7

Related Questions