I would like to refactor this piece of code which is not DRY at all 🙂
def my_method
if session[:my_params].try(:include?, :answer)
session[:my_params][:answer]
elsif session[:my_params].try(:include?, :question)
session[:my_params][:question]
end
end
Is there a way to implicitly return either :answer or :question depending on which one is present (if present) in session[:my params]?
A hash will return
nilif a key has no value associated in it. Becausenilandfalseare both handled the same in conditionals, we can shorten this method very aggressively. Theif session[:my_params]ensures that thesession[:my_params]is not nil.