This is the closest I've found but it deals with booleans instead of numbers: DRY up Ruby ternary
I'm trying to avoid a divide by 0 scenario in Ruby, while keeping the code shorter and easier to read.
I want to make the following bit of code more concise:
number_to_percentage ((percent_complete.nan? ? 0 : (percent_complete * 100)), :precision => 0)
or
number_to_percentage ((percent_complete.nan? ? 0 : percent_complete) * 100, :precision => 0)
It just seems really hard to read right now. There's more chaining involved so the percent_complete
is actually quite a bit longer. I'm also returning this value from a method, so it gets longer yet again.
I'd like to avoid using temporary variables or shortening the method names, so was hoping to learn of another way.
If I can avoid having to type "percent_complete" twice then that pretty much solves it.