Skip to main content

Ruby: regex scanning in a case statement

·57 words·1 min· ·
Ruby Snippet
Ariejan de Vroom
Author
Ariejan de Vroom
Jack of all Trades, Professional Software Craftsman

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.

Related

Rails Snippet: Caching expensive calls
·166 words·1 min
General RubyOnRails Ruby Rails Snippet
Running a different ruby with Passenger 3.2 and RVM
·262 words·2 mins
Ruby Rvm Passenger Nginx
Showing Ruby, Rails and git info in your app
·206 words·1 min
Ruby Rails Protip