Wednesday, October 14, 2009

Switch statement in ruby on rails

In other languages switch case statement use for avoiding many if else conditions. In ruby also use these type of tack-tick for avoid many if else conditions.

try this code

exapmle 1
case name
when "banusha"
#some_code
when "kamal"
#some_code
else
#...
end

example 2

case number
when 1
puts "number is equal to 1"
when 2..9
puts "number is between 2 and 9"
when 10
puts "number is equal to 10"
end

example 3

case number
when 1 then puts "number is equal to 1"
when 2..9 then puts "number is between 2 and 9"
when 10 then puts "number is equal to 10"
end
example 4

case number
when 1 ; puts "number is equal to 1"
when 2..9 ; puts "number is between 2 and 9"
when 10 ; puts "number is equal to 10"
end


No comments:

Post a Comment