Thursday, May 22, 2008

Switch case in Ruby

Swith case is support in Ruby language:

i=10
case i
when 1..9
puts "not 10"
when 10
puts "it is 10"
end
output: "it is 10"

String matching:

input="hello"

case input
when "hello world"
puts "hello world"
when "hell"
puts "heaven"
when /hell/
puts "hell inside"
end

output: "hell inside"

No comments: