From: Brian Candler Date: 2010-07-07T21:40:40+09:00 Subject: Re: Ruby switches Kenneth     wrote: > Let's say I have something like this > > case > when "".empty? then puts "Empty" > when "".nil? then puts "Nil" > when "".include?("a") then puts "Includes 'a'" > end > > Is there a way to do something like this instead? (This does not work > since the methods in the switches are acting on Object instead of "".) > > case "" > when empty? then puts "Empty" > when nil? then puts "Nil" > when include?("a") then puts "Includes 'a'" > end case str when "": puts "Empty" when nil: puts "Nil" when /a/: puts "Includes 'a'" end The case statement actually expands to the === operator, so this is equivalent to if "" === str puts "Empty" elsif nil === str puts "Nil" elsif /a/ === str puts "Includes 'a'" end The === operator does the "right thing" in each of these cases. -- Posted via http://www.ruby-forum.com/.