I currently have a piece of code that looks like this:
if match = request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/) match[:slug] end
Is there a way to use the safe navigation operator (introduced in 2.3.0) to avoid this if conditional?
if
Just use the regular (non-sugar) form.
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.[](:slug)
You can use send any method, so with a secure browser it will be:
send
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.send(:[], :slug)
For better readability, I chose #dig instead of #[] .
#dig
#[]
how
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.dig(:slug)
instead