Tag Archives: switch/case

Ruby: Case Testing Against Arrays of Values

Did you know that a when clause in a Ruby case statement can test against each item in an array? If any item in the array matches the case statement’s comparison (target) value, the when clause will evaluate to true.

In the below example, :express_truck will be returned if country matches any of the truckable_countries.

truckable_countries = ['United States', 'Canada', 'Mexico']

ship_via = case country
           when *truckable_countries
           	:express_truck
           else
           	:supersonic_jet
           end

Continue reading

Emulating C/C++’s switch Fall Through with C#’s goto

Today, while doing some research in the MSDN Library, I came across an interesting use of C#’s goto statement. Instead of causing the application’s execution to jump to a named label—a practice frowned upon in the programming world—this example used goto to emulate C/C++’s switch statement’s fall through behavior. Continue reading