Shortening an if-else structure in Ruby
ruby
Solution
For your case, something like this:
med_name = params[:medication_name]
med_name = 'all' if [nil, 'undefined'].include? med_name
For more general long chains of `if`/`elsif`/`else`, look at `case` statements. They're overkill in this case, but here's an example:
med_name = case params[:med_name]
when 'undefined', nil
'all'
else
params[:med_name]
end
Problem
I have written something like this, the same if-else logic I knew from Visual Basic 6.0, but I am sure there is a better "Ruby way" of writing it. Can you please show me how it would look like in Ruby world? ``` if params[:medication_name].nil? med_name = 'all' elsif params[:medication_name] == 'undefined' med_name = 'all' else med_name = params[:medication_name] end ```