u16suzuの blog

u16suzu のブログです。

Ruby useful methods; Object#try and Object#presence

Object#try

user ? user.name : 'no user'

it can be convert to

user.try(:name) || 'no user'

Object#presence

name = params[:name] ? params[:name] : 'no name'

it can be convert to

name = params[:name].presence || 'no name'