Ruby: regex scanning in a case statement

| Posted: - Last updated:
| Tagged ruby snippet
| ~100wrds (~1min)

Here’s a handy ruby snippet that might come in handy one day.

When the regex matches (input should end with " today"), you can directly grab the matched value using the special $1 variable.

case input
when /(.*)\stoday$/i then
  puts "Today: #{$1}"
end

I think you can see how you can bend this to your own needs.

My site is free of ads and trackers. I record privacy-respecting usage statistics with Fathom.

Was this post helpful to you? Why not ☕ Buy me a coffee or use Brave Browser to send a tip.