Technical Evaluations

4 ways to do conditionals in Ruby

Conditionals are more than just if/else and switch cases

Duong Nguyen
The Startup
Published in
3 min readJun 21, 2020

--

Photo by Jared Murray on Unsplash

There are multiple options when it comes to conditional flows in Ruby.

  • If/else
  • Switch Case
  • Hash Map
  • Polymorphism

However, none of them is either a silver bullet or a terrible choice.

In this article, we would discuss the PROS and CONS, and the suitable use cases for each approach.

1. If/else

If/else is the most basic block when it comes to conditional flows.

It is the most flexible approach which can be used to implement any conditional scenarios.

Scenario 1: Suggest positions for football players based on his/her statistics.

In the above example, multiple aspects (defense , dribble , passing , shooting , speed , crossing ) are being evaluated in the conditional statements. This makes…

--

--

Duong Nguyen
The Startup

Engineering Manager@ Rakuten Viki. All opinions are my own.