Ruby - Using case when in assignment

Description

Using case when in assignment

Demo

fruit = "orange" 
color = case fruit # w  w  w  .j av  a  2  s.  com
 when "orange" 
 "orange" 
 when "apple" 
 "green" 
 when "banana" 
 "yellow" 
 else 
 "unknown" 
end

Here, we use a case block, but you assign the result of whichever inner block is executed directly to color.

Related Topic