Ruby - Using case when in assignment

Description

Using case when in assignment

Demo

fruit = "orange" 
color = case fruit # ww  w.  j a  v a  2s .  c om
 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