Devin Mullins
2/8/2007 1:46:00 PM
Josselin wrote:
> if params[:new_title].blank?
> params[:new_title] = nil
> elsif params[:new_title] == "null"
> params[:new_title] = ""
> end
params[:new_title] = if params[:new_title].blank?
nil
elsif params[:new_title] == "null"
""
end
For reasons yet unbeknownst to you, reverse that if statement:
params[:new_title] = if params[:new_title] == "null"
""
elsif params[:new_title].blank?
nil
end
Implicit in that if statement is an "else nil". (Try it in IRB.) So you
don't really need anything but the first part:
params[:new_title] = if params[:new_title] == "null"
""
end
You can shorten even further by using the line-modifier version:
params[:new_title] = ("" if params[:new_title] == "null")
(Parentheses are important, here. Otherwise, the if modifies the whole
line, and if not "null", the :new_title remains unchanged.) But that's a
little greek. Here's another way:
params[:new_title] = params[:new_title] == 'null' ? '' : nil
Now, there's something very weird with the above logic, in the first
place. You might be able to reduce even more code by changing the thing
that sets :new_title. ("null", for Ford's sake?)
Devin