General
Method 1
The case statement is used as follows:
operation = 1
case operation
when 1
puts 'operation is successful'
when 0
puts 'operation is a failure'
end
The compiler will compare the value after 'when' , which is 1 with operation. If it evaluates to true, then the code after "when 1" is executed.
Results : "operation is successful"
Method 2
Another method of use is :
operation = 1
result = case operation
when 1
1 + 1
when 2
1 - 1
end
puts result
Here , result will print 2 because "1+1" is executed. This returns 2 and passed into "result" variable.
Method 1
The case statement is used as follows:
operation = 1
case operation
when 1
puts 'operation is successful'
when 0
puts 'operation is a failure'
end
The compiler will compare the value after 'when' , which is 1 with operation. If it evaluates to true, then the code after "when 1" is executed.
Results : "operation is successful"
Method 2
Another method of use is :
operation = 1
result = case operation
when 1
1 + 1
when 2
1 - 1
end
puts result
Here , result will print 2 because "1+1" is executed. This returns 2 and passed into "result" variable.
Comments
Post a Comment