Regular Expressions in Case Statements
suggest changeYou can test a string against several regular expressions with a case statement.
Example
case "Ruby is #1!"
when /\APython/
puts "Boooo."
when /\ARuby/
puts "You are right."
else
puts "Sorry, I didn't understand that."
end
case compares with ===, not ==. The when pattern is the receiver, so /\ARuby/ === "Ruby is #1!" is the test that runs. Regexp#=== is a match.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents