3 thoughts on “Conditional variable assignment.”

  1. I’m not sure if it will work but have you tried:
    params[:start_date] ? start = params[:start_date].to_date : start = Date.today

  2. `start = (params[:start_date]) ? params[:start_date].to_date:Date.today`

    /u/SaffronBelly might be close, but this is more concise.

    edit: the condition might be better as `(params.try(:start_date))` or `has_attribute` maybe, I haven’t tested it.

  3. We can’t call `to_date` on `nil` but we can call `to_date` on a `Date`.

    This allows us to do:

    start = (params[:start_date] ||= Date.today).to_date


Leave a Comment