Regular Expressions in Case Statements

suggest change

You 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents